Thursday, August 7, 2014

C# finding duplicates in a List using LINQ



Suppose you have a list that contains entries repeated more than once.
Using LINQ you can get the repeated elements and their values.

The easiest way to solve the problem is to group the elements based on their value, and then pick a rapresentative of the gorup if the elements in it are more than 1.
In linq, this translates to:
    var query = lst.GroupBy(x=>x)

              .Where(g=>g.Count()>1)

              .Select(y=>y.Key)

              .ToList();

You can replace x=>x with x => x.Property if your list is a list of complex objects and you want to search for a specific duplicate value. If you compare objects you need to override equals.
If you want to know how many times the elements are repeated, you can use:

    var query = lst.GroupBy(x=>x)

              .Where(g=>g.Count()>1)

              .Select(y=> new { Element = y.Key, Counter = g.Count()})

              .ToList();

Submit this story to DotNetKicks