May 29, 2014

Remove Duplicates from a List in C#

A List can contain simple types like int, string or other objects. Other day I was working and had to remove duplicates from a List collection. In this post, I will talk about how I remove the duplicates using minimum code. To start with a clue, I used a HashSet to remove the duplicates.

HashSet has the feature to only contain distinct items within it. Therefore, I could convert the List to HashSet and back to a list and this would remove all the duplicates for me. For HashSets to only contain unique items, the object needs to override the Equals and GetHashCode methods. Below is an example of how this can be done. I have created an Employee class with few members and overridden Equals and GetHashCode methods. Then I have another class EmployeeCollection where I have removed the duplicates.

Remove Duplicates
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace WebApplication3.Helper
  5. {
  6.     public class EmployeeCollection
  7.     {
  8.         private List<Employee> RemoveDuplicates()
  9.         {
  10.             List<Employee> list = new List<Employee>();
  11.             //load employees to list
  12.             //list.Add(new Employee() { FirstName = "John" });
  13.  
  14.             //HashSet removes duplicates
  15.             HashSet<Employee> emSet = new HashSet<Employee>(list);
  16.             return new List<Employee>(emSet);
  17.         }
  18.     }
  19.  
  20.     public class Employee
  21.     {
  22.         public string FirstName { get; set; }
  23.         public string LastName { get; set; }
  24.         public DateTime DateOfBirth { get; set; }
  25.         public DateTime DateJoined { get; set; }
  26.         public bool IsRetired { get; set; }
  27.  
  28.         public override string ToString()
  29.         {
  30.             return "First Name: " + FirstName + "\nLastName: " + LastName + "\nDate of Birth: " + DateOfBirth
  31.                 + "\nDate Joined: " + DateJoined + "\nIs Retired? " + IsRetired;
  32.         }
  33.  
  34.         public override bool Equals(object obj)
  35.         {
  36.             Employee em = (Employee)obj;
  37.             return this.FirstName == em.FirstName
  38.                 && this.LastName == em.LastName
  39.                 && this.DateOfBirth == em.DateOfBirth
  40.                 && this.DateJoined == em.DateJoined
  41.                 && this.IsRetired == em.IsRetired;
  42.         }
  43.  
  44.         public override int GetHashCode()
  45.         {
  46.             unchecked
  47.             {
  48.                 int hash = 23;
  49.  
  50.                 hash = FirstName == null ? hash * 11 : hash * 17 + FirstName.GetHashCode();
  51.                 hash = LastName == null ? hash * 19 : hash * 17 + LastName.GetHashCode();
  52.                 hash = DateOfBirth == null ? hash * 13 : hash * 17 + DateOfBirth.GetHashCode();
  53.                 hash = DateJoined == null ? hash * 7 : hash * 17 + DateJoined.GetHashCode();
  54.                 hash = IsRetired == null ? hash * 7 : hash * 17 + IsRetired.GetHashCode();
  55.                 return hash;
  56.             }
  57.         }  
  58.     }
  59. }

0 comments:

Reference: Shahed Kazi at AspNetify.com