How to Remove Duplicates in a C# Array?

In this C# tutorial, I will explain in detail, how to remove duplicates in a C# array.

To remove duplicates in a C# array, you can use any one of the below 3 methods.

  1. Using LINQ
  2. Using HashSet
  3. Using Manual Looping (for loop)

Remove Duplicates in a C# Array

Now, let us check, each of the methods with examples.

Method 1: Using LINQ

LINQ (Language Integrated Query) is a powerful feature in C# that allows you to query and manipulate data. To use LINQ, you’ll need to include the System.Linq namespace.

Here’s how you can remove duplicates using LINQ:

using System;
using System.Linq;

class Program {
    static void Main() {
        string[] usaNames = { "John", "Alice", "Bob", "John", "Alice", "Chris" };
        
        string[] uniqueNames = usaNames.Distinct().ToArray();
        
        foreach (var name in uniqueNames) {
            Console.WriteLine(name);
        }
    }
}

Once you run the code, you can see the output below:

John
Alice
Bob
Chris

Check out the screenshot below:

Remove Duplicates in a C# Array

Method 2: Using HashSet

A HashSet is a collection that does not allow duplicate elements. It is available under the System.Collections.Generic namespace.

Here’s how you can remove duplicates using a HashSet:

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        string[] usaNames = { "John", "Alice", "Bob", "John", "Alice", "Chris" };
        
        HashSet<string> hashSet = new HashSet<string>(usaNames);
        string[] uniqueNames = hashSet.ToArray();
        
        foreach (var name in uniqueNames) {
            Console.WriteLine(name);
        }
    }
}

Once you run the code, you can see the output below:

John
Alice
Bob
Chris

Method 3: using for loop

Sometimes, you might prefer a manual approach to remove duplicates, without relying on LINQ or external data structures. This involves looping through each element in the array and checking for duplicates in C#.

Here’s how to remove duplicates from an array using for loop in C#.

using System;

class Program {
    static void Main() {
        string[] usaNames = { "John", "Alice", "Bob", "John", "Alice", "Chris" };
        
        int arrayLength = usaNames.Length;
        string[] uniqueNames = new string[arrayLength];
        int uniqueCount = 0;

        for (int i = 0; i < arrayLength; i++) {
            bool isDuplicate = false;
            for (int j = 0; j < uniqueCount; j++) {
                if (usaNames[i] == uniqueNames[j]) {
                    isDuplicate = true;
                    break;
                }
            }

            if (!isDuplicate) {
                uniqueNames[uniqueCount] = usaNames[i];
                uniqueCount++;
            }
        }

        // Resize the array to remove unused slots
        Array.Resize(ref uniqueNames, uniqueCount);

        foreach (var name in uniqueNames) {
            Console.WriteLine(name);
        }
    }
}

You can see the output once you run the code in a console application like below:

How to Remove Duplicates in a C# Array

Conclusion

We discussed the different methods to remove duplicates in a C# array. Each method has its own advantages and disadvantages.

  • LINQ is the most straightforward, but may not be the most efficient for large arrays.
  • HashSet offers better performance but might consume more memory.
  • Manual looping gives you full control but may require more lines of code.

You may also like the following tutorials: