How to remove duplicates in a list of objects in C#

To remove duplicates in a list of objects in C#, you can use LINQ's Distinct() method along with a custom equality comparer. Here's an example:

  • Add the following using statement:
using System.Linq;
  • Define a sample class:
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}
  • Create a custom equality comparer for the Person class:
public class PersonEqualityComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        if (x == null && y == null)
            return true;
        if (x == null || y == null)
            return false;
        return x.Id == y.Id && x.Name == y.Name;
    }

    public int GetHashCode(Person obj)
    {
        if (obj == null)
            return 0;
        int hashId = obj.Id.GetHashCode();
        int hashName = obj.Name == null ? 0 : obj.Name.GetHashCode();
        return hashId ^ hashName;
    }
}

In this example, the PersonEqualityComparer class implements the IEqualityComparer<Person> interface and provides custom Equals() and GetHashCode() methods for comparing Person objects.

  • Create a list with duplicate objects and use Distinct() to remove duplicates:
List<Person> people = new List<Person>
{
    new Person { Id = 1, Name = "John" },
    new Person { Id = 2, Name = "Jane" },
    new Person { Id = 1, Name = "John" },
    new Person { Id = 3, Name = "Alice" },
    new Person { Id = 2, Name = "Jane" }
};

List<Person> distinctPeople = people.Distinct(new PersonEqualityComparer()).ToList();

foreach (Person person in distinctPeople)
{
    Console.WriteLine($"ID: {person.Id}, Name: {person.Name}");
}

In this example, the Distinct() method is called with the custom PersonEqualityComparer instance to remove duplicates from the people list based on the Id and Name properties.

Adjust the custom equality comparer and the Person class according to your needs. If you only want to remove duplicates based on a single property (e.g., Id), you can modify the Equals() and GetHashCode() methods in the PersonEqualityComparer class accordingly.

  1. Removing duplicate objects in a List<T> in C#:

    List<MyObject> uniqueList = originalList.Distinct().ToList();
    
  2. Remove duplicate items from List of custom objects in C#:

    List<MyObject> uniqueList = originalList.GroupBy(obj => obj.Id).Select(group => group.First()).ToList();
    
  3. C# LINQ remove duplicate objects from List:

    List<MyObject> uniqueList = originalList.DistinctBy(obj => obj.PropertyToCheck).ToList();
    
  4. Remove duplicate elements in a List<T> in C#:

    List<T> uniqueList = originalList.Distinct().ToList();
    
  5. C# remove duplicates from List of objects based on a property:

    List<MyObject> uniqueList = originalList.GroupBy(obj => obj.PropertyToCheck).Select(group => group.First()).ToList();
    
  6. Distinct operation on List of custom objects in C#:

    List<MyObject> uniqueList = originalList.Distinct(new MyObjectComparer()).ToList();
    
  7. C# remove duplicate objects using HashSet:

    HashSet<MyObject> uniqueSet = new HashSet<MyObject>(originalList);
    List<MyObject> uniqueList = uniqueSet.ToList();
    
  8. Remove duplicate items from ObservableCollection in C#:

    ObservableCollection<MyObject> uniqueCollection = new ObservableCollection<MyObject>(originalCollection.Distinct());
    
  9. C# remove duplicates from List with custom equality comparison:

    List<MyObject> uniqueList = originalList.Distinct(new MyObjectEqualityComparer()).ToList();
    
  10. Remove duplicate objects from Dictionary in C#:

    Dictionary<TKey, TValue> uniqueDictionary = originalDictionary.GroupBy(kv => kv.Value).ToDictionary(group => group.Key, group => group.First().Value);
    
  11. C# remove duplicates from List using LINQ:

    List<MyObject> uniqueList = originalList.GroupBy(obj => obj.PropertyToCheck).Select(group => group.First()).ToList();
    
  12. Remove duplicate objects from DataTable in C#:

    DataTable uniqueDataTable = originalDataTable.DefaultView.ToTable(true, "ColumnName1", "ColumnName2");
    
  13. C# remove duplicate items from HashSet:

    HashSet<T> uniqueSet = new HashSet<T>(originalList);
    
  14. Remove duplicates from List of objects in C# without LINQ:

    List<MyObject> uniqueList = new List<MyObject>();
    foreach (MyObject obj in originalList)
    {
        if (!uniqueList.Any(o => o.Equals(obj)))
        {
            uniqueList.Add(obj);
        }
    }
    
  15. C# remove duplicate elements from array of objects:

    MyObject[] uniqueArray = originalArray.Distinct(new MyObjectEqualityComparer()).ToArray();
    
  16. Remove duplicate items from ObservableCollection in C# using LINQ:

    ObservableCollection<MyObject> uniqueCollection = new ObservableCollection<MyObject>(originalCollection.GroupBy(obj => obj.PropertyToCheck).Select(group => group.First()));
    
  17. C# remove duplicates from List of objects with IEqualityComparer:

    List<MyObject> uniqueList = originalList.Distinct(new MyObjectEqualityComparer()).ToList();
    
  18. Remove duplicate objects from ConcurrentDictionary in C#:

    ConcurrentDictionary<TKey, TValue> uniqueDictionary = new ConcurrentDictionary<TKey, TValue>(originalDictionary);
    
  19. C# remove duplicate elements from array using HashSet:

    T[] uniqueArray = new HashSet<T>(originalArray).ToArray();