C# Program to Remove Duplicates from an Array

In this article, we will explore how to remove duplicate elements from an array in C# using different approaches. Removing duplicates is a common programming task that helps maintain data integrity and optimize storage.

What are Duplicate Elements in an Array?

Duplicate elements are values that appear more than once in an array. When removing duplicates, we keep only one occurrence of each unique value, creating a new array with distinct elements.

Examples

Input:
array = {1, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6}
Output:
array = {1, 2, 3, 4, 5, 6}
Explanation: After removing the duplicate elements, 
the remaining elements are 1, 2, 3, 4, 5, and 6.
Input:
array = {11, 13, 11, 15, 16, 14, 12, 12, 14, 16, 13, 15}
Output:
array = {11, 12, 13, 14, 15, 16}
Explanation: After removing the duplicate elements,
the remaining elements are 11, 12, 13, 14, 15, and 16.

Using HashSet to Remove Duplicates

The HashSet approach is the most efficient and straightforward method. A HashSet automatically maintains unique elements, making it perfect for removing duplicates. This method preserves the first occurrence of each element while eliminating subsequent duplicates.

HashSet Duplicate Removal Process Original Array 1 2 2 3 4 4 5 HashSet automatically removes duplicates Result Array 1 2 3 4 5 Time Complexity: O(n) | Space Complexity: O(n) Best approach for unsorted arrays with random duplicates

Example

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

class Program {
    static void Main() {
        int[] array = {1, 2, 2, 3, 4, 4, 4, 5};
        
        Console.WriteLine("Original array: " + string.Join(", ", array));
        
        HashSet<int> uniqueElements = new HashSet<int>();
        
        foreach (int num in array) {
            uniqueElements.Add(num);
        }
        
        int[] resultArray = uniqueElements.ToArray();
        
        Console.WriteLine("Array after removing duplicates: " + string.Join(", ", resultArray));
    }
}

The output of the above code is

Original array: 1, 2, 2, 3, 4, 4, 4, 5
Array after removing duplicates: 1, 2, 3, 4, 5

Using Array Sorting to Remove Duplicates

This approach first sorts the array, which groups duplicate elements together. We then iterate through the sorted array, adding only elements that differ from their predecessor to maintain uniqueness.

Example

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        int[] array = {4, 1, 2, 4, 2, 3, 1, 5};
        
        Console.WriteLine("Original array: " + string.Join(", ", array));
        
        Array.Sort(array);
        
        List<int> uniqueElements = new List<int>();
        
        if (array.Length > 0) {
            uniqueElements.Add(array[0]);
            
            for (int i = 1; i < array.Length; i++) {
                if (array[i] != array[i - 1]) {
                    uniqueElements.Add(array[i]);
                }
            }
        }
        
        int[] resultArray = uniqueElements.ToArray();
        
        Console.WriteLine("Sorted array: " + string.Join(", ", array));
        Console.WriteLine("Array after removing duplicates: " + string.Join(", ", resultArray));
    }
}

The output of the above code is

Original array: 4, 1, 2, 4, 2, 3, 1, 5
Sorted array: 1, 1, 2, 2, 3, 4, 4, 5
Array after removing duplicates: 1, 2, 3, 4, 5

Using LINQ Distinct Method

C# provides a built-in LINQ method called Distinct() that removes duplicates with minimal code. This is the most concise approach for removing duplicates.

Example

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] array = {1, 2, 2, 3, 4, 4, 4, 5};
        
        Console.WriteLine("Original array: " + string.Join(", ", array));
        
        int[] uniqueArray = array.Distinct().ToArray();
        
        Console.WriteLine("Array after removing duplicates: " + string.Join(", ", uniqueArray));
    }
}

The output of the above code is

Original array: 1, 2, 2, 3, 4, 4, 4, 5
Array after removing duplicates: 1, 2, 3, 4, 5

Comparison of Approaches

Approach Time Complexity Space Complexity Best Use Case
HashSet O(n) O(n) Fastest for unsorted data
Sorting O(n log n) O(n) When sorted output is needed
LINQ Distinct O(n) O(n) Most concise code

Conclusion

The HashSet approach offers the best performance for removing duplicates from unsorted arrays, while the LINQ Distinct method provides the most readable code. Choose the sorting approach when you need the final array to be sorted, as it accomplishes both tasks in one operation.

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

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements