Remove Duplicates From a List in C#: fix the manual loops, add objects and in-place, .NET 10 - #2269
Open
vladimir-pecanac-main wants to merge 3 commits into
Conversation
…in-place, net10.0
- Retarget all three projects to net10.0 and bump packages.
- Fix UsingIterationsAndShifting() and UsingIterationsAndSwapping(): both assigned
to a local variable and never wrote to the list, so { 1, 1, 2, 3, 4, 5 } came back
as { 1 }. Both now work on a copy and write back into it.
- Tests: a fixture with a duplicate at index 0 and one with duplicates spread through
the list, run against every method. WhenUsingDictionary now tests UsingDictionary().
- UsingRecursion() returns the list without duplicates when it reaches the end.
- where T : notnull clears CS8714 and CS8602.
- Add RemoveDuplicatesInPlace() (RemoveAll with a HashSet<T>), Person, PersonRecord
and PeopleHelper (Distinct on a class, on a record, and DistinctBy), with tests.
- Benchmark: [Params] for the number of distinct values in a 2,000 item list, method
names match the article, and the two methods that change their list start every
call from a fresh copy.
Sorting() compared every item with a start value of default, so a 0 in a
List<int> was never added: { 0, 1, 0, 2 } returned { 1, 2 }. The first item
is now always added. New theory test runs every method against { 0, 1, 0, 2 };
it fails on the old Sorting() and passes on all methods now.
With T only constrained to notnull, x.Equals(item) and list[i].Equals(list[j]) bind to object.Equals(object), so every comparison in the Any(), loop and sorting methods boxed an int. Constraining T to IEquatable<T> makes them call Equals(T) directly, which is what the same code does on a plain List<int>, so the benchmark now measures the code the article shows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sample for https://code-maze.com/remove-duplicates-from-a-list-csharp/
Only collections-lists/RemoveDuplicatesFromLists changes.
What changes:
Packages (re-queried on NuGet 2026-10-04, newest stable):
Local: SDK 10.0.302, runtime 10.0.10. dotnet build -c Release: 0 warnings, 0 errors. dotnet test: 41 passed, 0 failed. dotnet list package --vulnerable --include-transitive: none.