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.
Table of Contents
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:
- Program to copy all elements of an array into another array
- Program To Find Largest Number In An Array
- Remove Duplicate Elements from an Array
- Convert a 2D array into a 1D array
- Find the missing number in an array
- C# program to find the maximum and minimum number in an array
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- How to remove duplicate characters from a String in C#
- C# program to count the occurrences of each character in a String
- C# program to count vowels and consonants in a string
- C# Program to Check if a Given Number is Even or Odd
- Program to print prime numbers from 1 to N
- C# Program to rotate an array by k position
- C# Programs asked in Interviews
Latest posts by Shekh Ali (see all)
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024