Anyone have a quick method for de-duplicating a generic List in C#?
Use the Distinct() method to remove duplicates from a list in C#.
Place all items in a set and if the count of the set is different from the count of the list then there is a duplicate. Should be more efficient than Distinct as there is no need to go through all the list. Don't call list. Count() method.
If you're using .Net 3+, you can use Linq.
List<T> withDupes = LoadSomeData(); List<T> noDupes = withDupes.Distinct().ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With