How to remove duplicates from a list
The easiest way to remove duplicates from a list in C# is using LINQ's Distinct() method, which returns unique elements. Alternatively, convert the list to a HashSet<T> which automatically stores only unique values.
Distinct() preserves the order of first occurrence for each unique element. For custom objects, you may need to override GetHashCode() and Equals() or provide an IEqualityComparer<T>.
Both approaches have O(n) time complexity. HashSet is faster for large datasets and provides additional set operations like union and intersection.
C# Example Code
using System;
using System.Collections.Generic;
using System.Linq;
public class RemoveDuplicates
{
public static void Main(string[] args)
{
// Method 1: Using LINQ Distinct()
List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5, 1, 3 };
List<int> unique = numbers.Distinct().ToList();
Console.WriteLine("Original: " + string.Join(", ", numbers));
Console.WriteLine("Unique (Distinct): " + string.Join(", ", unique));
// Method 2: Using HashSet
List<int> numbers2 = new List<int> { 1, 2, 2, 3, 4, 4, 5, 1, 3 };
HashSet<int> uniqueSet = new HashSet<int>(numbers2);
List<int> uniqueList = uniqueSet.ToList();
Console.WriteLine("\nUnique (HashSet): " + string.Join(", ", uniqueList));
// Method 3: In-place with HashSet
List<string> names = new List<string> { "Alice", "Bob", "Alice", "Charlie", "Bob" };
HashSet<string> seen = new HashSet<string>();
List<string> uniqueNames = new List<string>();
foreach (string name in names)
{
if (seen.Add(name)) // Add returns false if already exists
{
uniqueNames.Add(name);
}
}
Console.WriteLine("\nUnique names: " + string.Join(", ", uniqueNames));
// With custom objects
List<Person> people = new List<Person>
{
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Alice", 25), // Duplicate
new Person("Charlie", 35),
new Person("Bob", 30) // Duplicate
};
// Using Distinct with custom comparer
var uniquePeople = people
.GroupBy(p => new { p.Name, p.Age })
.Select(g => g.First())
.ToList();
Console.WriteLine("\nUnique people:");
foreach (var person in uniquePeople)
{
Console.WriteLine($" {person.Name}: {person.Age}");
}
// Remove duplicates by specific property only
var uniqueByName = people
.GroupBy(p => p.Name)
.Select(g => g.First())
.ToList();
Console.WriteLine("\nUnique by name only:");
foreach (var person in uniqueByName)
{
Console.WriteLine($" {person.Name}: {person.Age}");
}
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}