C# program to print duplicates from a list of integers

To print duplicates from a list of integers in C#, you can use several approaches. The most common method involves using a Dictionary to count occurrences of each element, then filtering elements that appear more than once.

Using Dictionary with ContainsKey

This approach uses a Dictionary<int, int> to store each number and its count. The ContainsKey method checks if a number already exists in the dictionary −

using System;
using System.Collections.Generic;

class Program {
    public static void Main() {
        int[] arr = { 3, 6, 3, 8, 9, 2, 2, 5, 8 };
        
        var countDict = new Dictionary<int, int>();
        
        // Count occurrences of each number
        foreach (var num in arr) {
            if (countDict.ContainsKey(num))
                countDict[num]++;
            else
                countDict[num] = 1;
        }
        
        // Print only duplicates (count > 1)
        Console.WriteLine("Duplicate numbers:");
        foreach (var pair in countDict) {
            if (pair.Value > 1) {
                Console.WriteLine($"{pair.Key} occurs {pair.Value} times");
            }
        }
    }
}

The output of the above code is −

Duplicate numbers:
3 occurs 2 times
2 occurs 2 times
8 occurs 2 times

Using TryGetValue Method

A more efficient approach uses TryGetValue instead of ContainsKey to avoid double lookup −

using System;
using System.Collections.Generic;

class Program {
    public static void Main() {
        int[] arr = { 1, 2, 3, 2, 4, 1, 5, 3, 6 };
        
        var countDict = new Dictionary<int, int>();
        
        // Count occurrences using TryGetValue
        foreach (var num in arr) {
            if (countDict.TryGetValue(num, out int count))
                countDict[num] = count + 1;
            else
                countDict[num] = 1;
        }
        
        // Display duplicates
        Console.WriteLine("Numbers that appear more than once:");
        foreach (var pair in countDict) {
            if (pair.Value > 1) {
                Console.WriteLine($"Number {pair.Key}: {pair.Value} times");
            }
        }
    }
}

The output of the above code is −

Numbers that appear more than once:
2: 2 times
1: 2 times
3: 2 times

Using LINQ GroupBy

For a more concise solution, you can use LINQ's GroupBy method −

using System;
using System.Linq;

class Program {
    public static void Main() {
        int[] arr = { 7, 3, 7, 1, 3, 9, 1, 5 };
        
        var duplicates = arr
            .GroupBy(x => x)
            .Where(g => g.Count() > 1)
            .Select(g => new { Number = g.Key, Count = g.Count() });
        
        Console.WriteLine("Duplicate elements:");
        foreach (var item in duplicates) {
            Console.WriteLine($"{item.Number} appears {item.Count} times");
        }
    }
}

The output of the above code is −

Duplicate elements:
7 appears 2 times
3 appears 2 times
1 appears 2 times

Comparison of Methods

Method Performance Readability Memory Usage
Dictionary with ContainsKey Good High Moderate
Dictionary with TryGetValue Better High Moderate
LINQ GroupBy Moderate Very High Higher

Conclusion

Finding duplicates in an integer list can be accomplished using Dictionary collections or LINQ methods. The Dictionary approach with TryGetValue offers the best performance, while LINQ GroupBy provides the most readable and concise solution for smaller datasets.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements