This post will discuss how to find duplicates in a list in C#.

1. Using Enumerable.GroupBy() method

We can use the Enumerable.GroupBy() method to group the elements based on their value, then filters out the groups that appear only once, leaving them out with duplicates keys.

Download  Run Code

 
The above code can be shortened using the SelectMany() method:

Download  Run Code

 
If you prefer LINQ’s query syntax over the method syntax, create query expressions, as shown in the following example:

Download  Run Code

 
We can also create a dictionary that stores count the frequency of the duplicate elements in a list. The following code example shows how to implement the frequency map:

Download  Run Code

2. Using HashSet

Finally, we can also use HashSet<T>, as demonstrated below:

Download  Run Code

That’s all about finding the duplicates in a List in C#.