Find duplicate elements in a List in C#
This post will discuss how to find the duplicate elements in a list in C#.
1. Using Enumerable.GroupBy() Method
The idea is to use the Enumerable.GroupBy() method to group the elements based on their value, then filter out the groups that appear more than once, and retrieve the duplicates keys. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8 }; List<int> result = list.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(x => x.Key) .ToList(); Console.WriteLine(String.Join(", ", result)); // 5, -1 } } |
The code can be shortened using the Enumerable.SelectMany() method. However, to get all distinct duplicates, consider applying the Distinct() method to the resulting sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8, 5 }; var duplicates = list.GroupBy(x => x) .SelectMany(g => g.Skip(1)) .Distinct() .ToList(); Console.WriteLine(String.Join(", ", duplicates)); // 5, -1 } } |
To find the frequency of the repeated elements, you can map each element to contain Element and Count properties. This can be used to retrieve the desired information.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8 }; var result = list.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(x => new { Element = x.Key, Count = x.Count() }) .ToList(); Console.WriteLine(String.Join(", ", result)); } } |
Output:
{ Element = 5, Count = 2 }, { Element = -1, Count = 2 }
Alternatively, if you need a dictionary with the duplicate element as a key and the duplicate element’s count as its value, do as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8 }; var result = list.GroupBy(x => x) .Where(g => g.Count() > 1) .ToDictionary(x => x.Key, x => x.Count()); Console.WriteLine(String.Join(", ", result)); // [5, 2], [-1, 2] } } |
To determine whether a container contains any duplicate values or not, you can use the Enumerable.Any() method. The following code example returns true if the source sequence contains duplicated elements; otherwise, false.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8 }; var hasDuplicates = list.GroupBy(x => x).Any(g => g.Count() > 1); Console.WriteLine(hasDuplicates); // True } } |
Alternatively, you can check if the count of all distinct elements is exactly 1. This can be done using the Enumerable.All() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8 }; var isDistinct = list.GroupBy(x => x).All(g => g.Count() == 1); Console.WriteLine(isDistinct); // False } } |
2. Using Set
The idea here is to iterate over the list and keep track of all the items in a HashSet. If an item is encountered before, mark it as duplicate and report all duplicate items at the end of the loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 5, 9, -1, 8 }; var seen = new HashSet<int>(); var duplicates = list.Where(x => !seen.Add(x)); Console.WriteLine(String.Join(", ", duplicates)); // 5, -1 } } |
That’s all about finding the duplicate elements 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 :)