0
0
CsharpProgramBeginner · 2 min read

C# program to remove duplicates from array

You can remove duplicates from an array in C# by using a HashSet to store unique elements and then converting it back to an array, like var unique = new HashSet(array).ToArray();.
📋

Examples

Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 2, 3, 4, 5]
Input[10, 10, 10, 10]
Output[10]
Input[]
Output[]
🧠

How to Think About It

To remove duplicates, think of collecting only one copy of each number. You can use a special container that automatically ignores repeats, like a HashSet. Add each item from the array to this container, then get all unique items back as an array.
📐

Algorithm

1
Get the input array.
2
Create an empty set to hold unique elements.
3
Add each element from the array to the set (duplicates are ignored).
4
Convert the set back to an array.
5
Return or print the new array without duplicates.
💻

Code

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

class Program {
    static void Main() {
        int[] array = {1, 2, 2, 3, 4, 4, 5};
        int[] unique = new HashSet<int>(array).ToArray();
        Console.WriteLine("[{0}]", string.Join(", ", unique));
    }
}
Output
[1, 2, 3, 4, 5]
🔍

Dry Run

Let's trace the example array [1, 2, 2, 3, 4, 4, 5] through the code.

1

Input array

array = [1, 2, 2, 3, 4, 4, 5]

2

Create HashSet

hashSet = {} (empty set)

3

Add elements to HashSet

Add 1 -> {1} Add 2 -> {1, 2} Add 2 (duplicate) ignored Add 3 -> {1, 2, 3} Add 4 -> {1, 2, 3, 4} Add 4 (duplicate) ignored Add 5 -> {1, 2, 3, 4, 5}

4

Convert HashSet to array

unique = [1, 2, 3, 4, 5]

5

Print result

Output: [1, 2, 3, 4, 5]

IterationElement AddedHashSet Content
11{1}
22{1, 2}
32 (ignored){1, 2}
43{1, 2, 3}
54{1, 2, 3, 4}
64 (ignored){1, 2, 3, 4}
75{1, 2, 3, 4, 5}
💡

Why This Works

Step 1: Using HashSet

A HashSet automatically stores only unique values, so adding duplicates has no effect.

Step 2: Adding elements

When you add each array element to the HashSet, duplicates are ignored silently.

Step 3: Converting back to array

After collecting unique elements, converting the HashSet back to an array gives the final result without duplicates.

🔄

Alternative Approaches

Using LINQ Distinct()
csharp
using System;
using System.Linq;

class Program {
    static void Main() {
        int[] array = {1, 2, 2, 3, 4, 4, 5};
        int[] unique = array.Distinct().ToArray();
        Console.WriteLine("[{0}]", string.Join(", ", unique));
    }
}
This method is concise and readable but internally uses a set-like structure.
Manual loop with List and Contains
csharp
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        int[] array = {1, 2, 2, 3, 4, 4, 5};
        List<int> uniqueList = new List<int>();
        foreach (int num in array) {
            if (!uniqueList.Contains(num)) {
                uniqueList.Add(num);
            }
        }
        Console.WriteLine("[{0}]", string.Join(", ", uniqueList));
    }
}
This approach is simple but slower for large arrays because <code>Contains</code> checks each element.

Complexity: O(n) time, O(n) space

Time Complexity

Adding each element to a HashSet is O(1) on average, so total time is O(n) for n elements.

Space Complexity

Extra space is needed for the HashSet to store unique elements, which can be up to O(n).

Which Approach is Fastest?

Using HashSet or Distinct() is fastest and cleanest; manual loops with Contains are slower (O(n²)) for large arrays.

ApproachTimeSpaceBest For
HashSetO(n)O(n)Fast and efficient for all sizes
LINQ Distinct()O(n)O(n)Clean and concise code
Manual loop with ContainsO(n²)O(n)Small arrays or learning purposes
💡
Use HashSet or Distinct() for clean and efficient duplicate removal.
⚠️
Trying to remove duplicates by modifying the array while iterating can cause errors or skip elements.