Find duplicates in a List in C#
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 3, 2, 7, 7, 5, 6}; IEnumerable<int> duplicates = list.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(x => x.Key); Console.WriteLine("Duplicate elements are: " + String.Join(",", duplicates)); } } /* Output: Duplicate elements are: 3,5,7 */ |
The above code can be shortened using the SelectMany() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 3, 2, 7, 7, 5, 6}; IEnumerable<int> duplicates = list.GroupBy(x => x) .SelectMany(g => g.Skip(1)); Console.WriteLine("Duplicate elements are: " + String.Join(",", duplicates)); } } /* Output: Duplicate elements are: 3,5,7 */ |
If you prefer LINQ’s query syntax over the method syntax, create query expressions, as shown in the following example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 3, 2, 7, 7, 5, 6}; IEnumerable<int> duplicates = from x in list group x by x into g where g.Count() > 1 select g.Key; Console.WriteLine("Duplicate elements are: " + String.Join(",", duplicates)); } } /* Output: Duplicate elements are: 3,5,7 */ |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 3, 2, 7, 7, 5, 6}; Dictionary<int, int> freqMap = list.GroupBy(x => x) .Where(g => g.Count() > 1) .ToDictionary(x => x.Key, x => x.Count()); Console.WriteLine("[Value, Count]: " + String.Join(",", freqMap)); } } /* Output: [Value, Count]: [3, 2],[5, 2],[7, 2] */ |
2. Using HashSet
Finally, we can also use HashSet<T>, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int>() {3, 5, 3, 2, 7, 7, 5, 6}; HashSet<int> hashset = new HashSet<int>(); IEnumerable<int> duplicates = list.Where(e => !hashset.Add(e)); Console.WriteLine("Duplicate elements are: " + String.Join(",", duplicates)); } } /* Output: Duplicate elements are: 3,7,5 */ |
That’s all about finding the duplicates in a List in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)