C# Program to Find Duplicate Elements in Array
Dictionary<int,int> to count occurrences and print elements with count greater than one, like foreach(var item in dict) if(item.Value > 1) Console.WriteLine(item.Key);.Examples
How to Think About It
Algorithm
Code
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {1, 2, 2, 3, 4, 4, 5}; var counts = new Dictionary<int, int>(); foreach (int num in arr) { if (counts.ContainsKey(num)) counts[num]++; else counts[num] = 1; } bool found = false; foreach (var item in counts) { if (item.Value > 1) { if (!found) { Console.Write("Duplicate elements: "); found = true; } Console.Write(item.Key + " "); } } if (!found) Console.WriteLine("No duplicates found"); else Console.WriteLine(); } }
Dry Run
Let's trace the array [1, 2, 2, 3, 4, 4, 5] through the code
Initialize dictionary
counts = {}
Process element 1
counts = {1:1}
Process element 2
counts = {1:1, 2:1}
Process element 2 again
counts = {1:1, 2:2}
Process element 3
counts = {1:1, 2:2, 3:1}
Process element 4
counts = {1:1, 2:2, 3:1, 4:1}
Process element 4 again
counts = {1:1, 2:2, 3:1, 4:2}
Process element 5
counts = {1:1, 2:2, 3:1, 4:2, 5:1}
Check duplicates
Duplicates found: 2, 4
| Element | Dictionary State |
|---|---|
| 1 | {1:1} |
| 2 | {1:1, 2:1} |
| 2 | {1:1, 2:2} |
| 3 | {1:1, 2:2, 3:1} |
| 4 | {1:1, 2:2, 3:1, 4:1} |
| 4 | {1:1, 2:2, 3:1, 4:2} |
| 5 | {1:1, 2:2, 3:1, 4:2, 5:1} |
Why This Works
Step 1: Counting elements
We use a Dictionary to count how many times each element appears by increasing the count each time we see the element.
Step 2: Finding duplicates
After counting, elements with counts greater than one are duplicates because they appear multiple times.
Step 3: Output duplicates
We print all elements with count more than one to show the duplicates found in the array.
Alternative Approaches
using System; using System.Linq; class Program { static void Main() { int[] arr = {1, 2, 2, 3, 4, 4, 5}; var duplicates = arr.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key); if (duplicates.Any()) { Console.Write("Duplicate elements: "); foreach (var d in duplicates) Console.Write(d + " "); Console.WriteLine(); } else { Console.WriteLine("No duplicates found"); } } }
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {1, 2, 2, 3, 4, 4, 5}; var seen = new HashSet<int>(); var duplicates = new HashSet<int>(); foreach (int num in arr) { if (!seen.Add(num)) duplicates.Add(num); } if (duplicates.Count > 0) { Console.Write("Duplicate elements: "); foreach (var d in duplicates) Console.Write(d + " "); Console.WriteLine(); } else { Console.WriteLine("No duplicates found"); } } }
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through the array once, so time grows linearly with input size.
Space Complexity
Extra space is used for the dictionary to store counts, which can grow up to the size of the array.
Which Approach is Fastest?
All approaches run in linear time; using HashSet is slightly faster for just detecting duplicates without counts.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Dictionary Counting | O(n) | O(n) | Counting duplicates with frequency |
| LINQ GroupBy | O(n) | O(n) | Concise code with built-in methods |
| HashSet Tracking | O(n) | O(n) | Fast detection of duplicates without counts |