Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicates from a List<T> in C#

Anyone have a quick method for de-duplicating a generic List in C#?

like image 845
JC Grubbs Avatar asked Sep 06 '08 19:09

JC Grubbs


People also ask

How do I remove duplicates from a list in C sharp?

Use the Distinct() method to remove duplicates from a list in C#.

Can list have duplicates 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.


1 Answers

If you're using .Net 3+, you can use Linq.

List<T> withDupes = LoadSomeData(); List<T> noDupes = withDupes.Distinct().ToList(); 
like image 100
Factor Mystic Avatar answered Oct 19 '22 04:10

Factor Mystic