Find duplicates in an array in C#
This post will discuss how to find all duplicates in an array in C#.
1. Using Enumerable.GroupBy Method
The idea is to group the elements based on their value and then filter the groups that appear more than once. This can be done with LINQ’s Enumerable.GroupBy() method.
The following example shows how to use GroupBy to find all repeated values in an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; public class Example { public static void Main() { int[] arr = { 1, 2, 3, 2, 4, 5, 2, 4 }; var duplicates = arr.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(y => y.Key) .ToList(); Console.WriteLine(String.Join(", ", duplicates)); // 2, 4 } } |
To get the frequency of the repeated elements, you can map each element of the group to have the properties Item and Count. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; public class Example { public static void Main() { int[] arr = { 1, 2, 3, 2, 4, 5, 2, 4 }; var duplicates = arr.GroupBy(x => x) .Where(g => g.Count() > 1) .Select(y => new { Item = y.Key, Count = y.Count() }) .ToList(); Console.WriteLine(String.Join("\n", duplicates)); } } |
Output:
{ Item = 2, Count = 3 }
{ Item = 4, Count = 2 }
Alternatively, you can construct a Dictionary<TKey,TValue> from an IEnumerable<T> with the Enumerable.ToDictionary() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Linq; public class Example { public static void Main() { int[] arr = { 1, 2, 3, 2, 4, 5, 2, 4 }; var duplicates = arr.GroupBy(x => x) .Where(g => g.Count() > 1) .ToDictionary(x => x.Key, y => y.Count()); Console.WriteLine(String.Join(", ", duplicates)); } } |
Output:
[2, 3], [4, 2]
2. Using HashSet
Another option is to iterate over elements in the array and insert each element in a HashSet. If the current element already exists in the set, then it is a duplicate. This is demonstrated below:
|
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() { int[] arr = { 1, 2, 3, 2, 4, 5, 2, 4 }; var set = new HashSet<int>(); var duplicates = arr.Where(i => !set.Add(i)).Distinct(); Console.WriteLine(String.Join(", ", duplicates)); // 2, 4 } } |
3. Using Enumerable.Distinct Method
If you need to check the presence of only duplicate elements in the array, remove duplicate elements from it, and get the size of the result set. The Enumerable.Distinct method returns distinct elements from the sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Linq; public class Example { public static void Main() { int[] arr = { 1, 2, 3, 2, 4, 5, 2, 4 }; if (arr.Length != arr.Distinct().Count()) { Console.WriteLine("Array contains duplicates"); } else { Console.WriteLine("Array does not contains duplicates"); } } } |
Output:
Array contains duplicates
That’s all about finding duplicates in an array 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 :)