Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to Remove Duplicate Value from a list<> by lambda

what is fastest way to remove duplicate values from a list. Assume List<long> longs = new List<long> { 1, 2, 3, 4, 3, 2, 5 }; So I am interesting in use lambda to remove duplicate and returned : {1, 2, 3, 4, 5}. What is your suggestion?

like image 896
Saeid Avatar asked May 17 '12 09:05

Saeid


People also ask

What is the simplest way to delete duplicate elements from a list?

The method unique() from Numpy module can help us remove duplicate from the list given. The Pandas module has a unique() method that will give us the unique elements from the list given. The combination of list comprehension and enumerate is used to remove the duplicate elements from the list.

How do you remove duplicates with the highest value?

1. If you want to remove all duplicates but leave the highest ones, you can apply this formula =MAX(IF($A$2:$A$12=D2,$B$2:$B$12)), remember to press Shift + Ctrl + Enter keys. 2. In the above formulas, A2:A12 is the original list you need to remove duplicates from.


2 Answers

The easiest way to get a new list would be:

List<long> unique = longs.Distinct().ToList(); 

Is that good enough for you, or do you need to mutate the existing list? The latter is significantly more long-winded.

Note that Distinct() isn't guaranteed to preserve the original order, but in the current implementation it will - and that's the most natural implementation. See my Edulinq blog post about Distinct() for more information.

If you don't need it to be a List<long>, you could just keep it as:

IEnumerable<long> unique = longs.Distinct(); 

At this point it will go through the de-duping each time you iterate over unique though. Whether that's good or not will depend on your requirements.

like image 87
Jon Skeet Avatar answered Sep 30 '22 04:09

Jon Skeet


You can use this extension method for enumerables containing more complex types:

IEnumerable<Foo> distinctList = sourceList.DistinctBy(x => x.FooName);  public static IEnumerable<TSource> DistinctBy<TSource, TKey>(     this IEnumerable<TSource> source,     Func<TSource, TKey> keySelector) {     var knownKeys = new HashSet<TKey>();     return source.Where(element => knownKeys.Add(keySelector(element))); } 
like image 20
Jon Rea Avatar answered Sep 30 '22 03:09

Jon Rea