How to remove duplicates from a list in C#

A duplicate is an element the same as another element in the list. In C#, various ways exist to remove duplicates from a list.

Let's explore three different ways to remove duplicates from a list.

Using the Distinct() method

The Distinct() method of the System.Linq namespace can be used to remove duplicates from a list. This technique will return a new IEnumerable that contains distinct elements from the source sequence.

The Distinct() method returns a new sequence that contains only distinct elements from the original sequence. In the example below, we'll use the Distinct() method to remove duplicates from the numbers list and then convert the result to a list using the ToList() method.

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 3, 4, 5, 5, 6 };
IEnumerable<int> distinctNumbers = numbers.Distinct().ToList();
Console.WriteLine("Original list:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
Console.WriteLine("\nList with duplicates removed:");
foreach (int number in distinctNumbers)
{
Console.Write(number + " ");
}
}
}

Using a HashSet

A HashSet is a data structure that doesn't contain duplicates. Since a HashSet only stores unique elements, this will automatically remove any duplicates from the given list. After adding all the elements to the HashSet, it can be converted back to a list using the ToList() function.

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 3, 4, 5, 5, 6 };
HashSet<int> distinctNumbers = new HashSet<int>(numbers);
List<int> distinctList = distinctNumbers.ToList();
Console.WriteLine("Original list:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
Console.WriteLine("\nList with duplicates removed:");
foreach (int number in distinctList)
{
Console.Write(number + " ");
}
}
}

Using a loop

We can also manually remove duplicates from a list using a loop. We'll iterate over each element in the list and compare it to other elements to check if it is a duplicate. If yes, it is removed from the list.

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 3, 4, 5, 5, 6 };
List<int> distinctNumbers = new List<int>();
foreach (int number in numbers)
{
if (!distinctNumbers.Contains(number))
{
distinctNumbers.Add(number);
}
}
Console.WriteLine("Original list:");
foreach (int number in numbers)
{
Console.Write(number + " ");
}
Console.WriteLine("\nList with duplicates removed:");
foreach (int number in distinctNumbers)
{
Console.Write(number + " ");
}
}
}

Conclusion

There are various ways to remove duplicates from a list. These methods are easy to apply and can be used to eliminate duplicates from a list successfully. However, it is important to understand the distinctions between the approaches and identify the one that best meets the requirement of the task.

Free Resources