C#  

How to Remove Duplicate Values from a List in C#

When working with collections in C#, removing duplicate values is a very common requirement. Whether you are processing user input, filtering database records, or cleaning API data, duplicates can cause incorrect results and performance issues. Fortunately, C# provides simple and efficient ways to remove duplicate values from a list.

In this article, we will look at the two most important and commonly used methods to remove duplicates from a list in C#.

Method 1: Using Distinct() (Recommended)

The easiest and most popular way to remove duplicates from a list in C# is by using the Distinct() method from LINQ. This method automatically filters out duplicate values and returns only unique elements.

Here is a simple example:

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

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 2, 4, 1, 5 };
        List<int> uniqueNumbers = numbers.Distinct().ToList();

        Console.WriteLine("Unique values:");
        foreach (var number in uniqueNumbers)

        {
            Console.WriteLine(number);

        }
    }
}

In this example, the original list contains duplicate values such as 1 and 2. The Distinct() method removes the duplicates and returns only unique values.

This approach is:

  • Simple

  • Readable

  • Efficient

Recommended for most real-world applications

If you are working with strings instead of integers, the same method works perfectly.

Method 2: Using HashSet

Another efficient way to remove duplicates is by using a HashSet. A HashSet automatically stores only unique values. If you try to add a duplicate value, it simply ignores it.

Here is the example:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 2, 4, 1, 5 };

        HashSet<int> uniqueSet = new HashSet<int>(numbers);

        List<int> uniqueNumbers = new List<int>(uniqueSet);

        Console.WriteLine("Unique values:");
        foreach (var number in uniqueNumbers)

        {
            Console.WriteLine(number);
        }
    }
}

The HashSet constructor takes the list as input and automatically removes duplicates.

This method is:

  • Very fast for large datasets

  • Memory efficient

  • Commonly used in performance-critical applications

Which Method Should You Use?

For most cases, Distinct() is the best choice because it is clean and easy to understand. However, if performance is critical and you are working with large collections, HashSet can be slightly faster.

Real-World Use Cases

Removing duplicates is commonly required in:

  • Filtering user input

  • Processing CSV or Excel data

  • Cleaning API response data

  • Preparing reports

  • Database result processing

Conclusion

Removing duplicate values from a list in C# is simple and efficient. The two most important methods are:

Using Distinct() (recommended and most readable)

Using HashSet (fast and efficient for large data)

Understanding these two approaches will help you handle data collections more effectively in real-world applications.