Removing duplicates from a list in C# is one of those everyday programming tasks that looks simple at first glance, but quickly becomes more interesting when you consider performance, ordering, custom objects, and case sensitivity. Whether you are cleaning user input, merging datasets, processing log entries, or preparing records for a database, knowing how to remove duplicates from a list in C# correctly can help you write cleaner, faster, and more reliable code. This guide walks through the most practical approaches, explains when to use each one, and shows how to handle common edge cases with clear examples
For more complex scenarios involving custom objects, the Distinct method requires a custom comparer. By implementing IEqualityComparer<T>, you can define exactly what constitutes a "duplicate" for your specific class. To give you an idea, you might consider two Person objects duplicates if they share the same email address, regardless of their names or IDs It's one of those things that adds up..
public class Person
{
public string Name { get; set; }
public string Email { get; set; }
}
public class PersonEmailComparer : IEqualityComparer
{
public bool Equals(Person x, Person y)
{
return x?.Email == y?.Email;
}
public int GetHashCode(Person obj)
{
return obj?.Here's the thing — email?. GetHashCode() ??
// Usage
var uniquePeople = peopleList.Distinct(new PersonEmailComparer());
This approach is highly efficient for large collections, as the GetHashCode method allows the underlying collection to quickly bucket items for comparison That's the part that actually makes a difference. Nothing fancy..
Every time you need to preserve the original order of first occurrence, the HashSet<T> is your best friend. It's a collection that inherently contains no duplicates and offers O(1) lookup time. The pattern is straightforward: iterate through your list, adding each item to a new list only if it can be added to a hash set.
var uniqueList = new List();
var seen = new HashSet();
foreach (var item in originalList)
{
if (seen.Add(item))
{
uniqueList.Add(item);
}
}
This method is not only performant but also very readable, making it a favorite for deduplication while maintaining order.
For ultimate flexibility, especially when dealing with data from different sources, LINQ's GroupBy clause is a powerful tool. It allows you to group items by a specific key and then select a representative from each group, effectively removing duplicates based on that key.
var uniqueRecords = records
.GroupBy(r => r.SomeKey)
.Select(g => g.First())
.ToList();
This is particularly useful when you want to apply additional logic, like selecting the record with the most recent timestamp from each group Small thing, real impact..
Conclusion: The "best" way to remove duplicates in C# hinges on your specific context. For simple types, Distinct() is concise. For custom objects, a custom comparer provides precise control. When order matters, the HashSet pattern is efficient and clear. And for complex grouping logic, GroupBy offers unparalleled flexibility. By understanding these tools, you can ensure your data is clean and your code is optimized for the task at hand.