C# program to find all duplicate elements in an integer array

In this article, we will learn how to write C# program to find all duplicate elements in an integer array and display how many times they occurred. We will write multiple C# code examples with proper code explanation.

Method1: Find all duplicate elements in an integer array using Dictionary

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
      int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 8};
      Dictionary<int, int> duplicates = new Dictionary<int, int>();

foreach (var value in array)
{
    if (duplicates.ContainsKey(value))
    {
        duplicates[value]++;
    }
    else
    {
        duplicates[value] = 1;
    }
}
foreach (var pair in duplicates)
{
    if (pair.Value > 1)
    {
     Console.WriteLine($"{pair.Key} appears {pair.Value} times");
    }
}
    }
}

Output:

10 appears 2 times
5 appears 3 times
2 appears 2 times
8 appears 2 times

Code Explanation:

  • We initialize a Dictionary duplicates to store the Occurrence of each element.
  • We iterate through the array, checking if the element exists in the Dictionary.
  • If the element exists, we increment its count; otherwise, we add it to the Dictionary with a count of 1.
  • Finally, we iterate through the Dictionary to display duplicates elements with a Occurrence greater than 1.

Method 2: Find all duplicate elements in an integer array Using GroupBy

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
      int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 8};
        var duplicates = array.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => new { Value = x.Key,    Count = x.Count() });
     foreach (var duplicate in duplicates)
     {
    Console.WriteLine($"{duplicate.Value} appears {duplicate.Count} times");
     }
    }
}

Output:

10 appears 2 times
5 appears 3 times
2 appears 2 times
8 appears 2 times

Code Explanation:

  • We use the GroupBy method to group Array elements by their value.
  • Then, we filter groups with a count greater than 1 using the Where method.
  • Finally, we project the element and its count using Select and display the results.

Method 3: Using HashSet

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
      int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 8};
        HashSet<int> set = new HashSet<int>();
        var duplicates = array.Where(i => !set.Add(i)).Distinct();
     Console.WriteLine(String.Join(", ", duplicates));
    }
}

Output:

10, 2, 5, 8

Conclusion:

In this article, we learned how to find all duplicate elements in an integer array using C# programming language. We use three different methods with proper code explanation. We hope this article was helpful to you.

Recommended Articles:

Shekh Ali
5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments