C# program to print all distinct elements of a given integer array in C#

Finding distinct elements in an array is a common programming task in C#. There are several approaches to accomplish this, including using Dictionary, HashSet, and LINQ methods. Each approach has its own advantages depending on your specific requirements.

Using Dictionary to Count Occurrences

A Dictionary<int, int> allows us to store each element as a key and its occurrence count as the value. This approach is useful when you need both distinct elements and their frequencies −

using System;
using System.Collections.Generic;

class Program {
    public static void Main() {
        int[] arr = { 88, 23, 56, 96, 43, 23, 88, 56 };
        var dictionary = new Dictionary<int, int>();
        
        foreach(var element in arr) {
            if (dictionary.ContainsKey(element))
                dictionary[element]++;
            else
                dictionary[element] = 1;
        }
        
        Console.WriteLine("Distinct elements with their occurrence count:");
        foreach(var pair in dictionary) {
            Console.WriteLine("{0} occurred {1} time(s)", pair.Key, pair.Value);
        }
    }
}

The output of the above code is −

Distinct elements with their occurrence count:
88 occurred 2 time(s)
23 occurred 2 time(s)
56 occurred 2 time(s)
96 occurred 1 time(s)
43 occurred 1 time(s)

Using HashSet for Simple Distinct Elements

When you only need distinct elements without counting occurrences, HashSet<T> is more efficient as it automatically handles uniqueness −

using System;
using System.Collections.Generic;

class Program {
    public static void Main() {
        int[] arr = { 88, 23, 56, 96, 43, 23, 88, 56 };
        var distinctElements = new HashSet<int>(arr);
        
        Console.WriteLine("Distinct elements using HashSet:");
        foreach(int element in distinctElements) {
            Console.WriteLine(element);
        }
    }
}

The output of the above code is −

Distinct elements using HashSet:
88
23
56
96
43

Using LINQ Distinct Method

LINQ provides a concise way to get distinct elements using the Distinct() method −

using System;
using System.Linq;

class Program {
    public static void Main() {
        int[] arr = { 88, 23, 56, 96, 43, 23, 88, 56 };
        var distinctElements = arr.Distinct();
        
        Console.WriteLine("Distinct elements using LINQ:");
        foreach(int element in distinctElements) {
            Console.WriteLine(element);
        }
    }
}

The output of the above code is −

Distinct elements using LINQ:
88
23
56
96
43

Comparison of Methods

Method Best For Performance Memory Usage
Dictionary When you need occurrence counts O(n) Higher (stores key-value pairs)
HashSet Simple distinct elements O(n) Lower (stores only elements)
LINQ Distinct Clean, readable code O(n) Moderate

Conclusion

There are multiple ways to find distinct elements in C# arrays. Use Dictionary when you need occurrence counts, HashSet for simple distinct elements with better performance, and LINQ's Distinct() method for clean and readable code. Choose the method that best fits your specific requirements.

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

639 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements