Working With HashSet In C#

The C# HashSet<T> class provides high-performance set operations. Here learn how to use HashSet in C#.

C# HashSet is an unordered collection of the unique elements. It was introduced in .NET 3.5 and is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. In this article, I will begin with creating a simple HashSet, and continue to perform the various operations on HashSet. At the end of the article, I will show how to create a HashSet of custom type and how to prevent the duplicates from being inserted in HashSet.

Let's begin our example:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace HashSetDemo {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             HashSet < string > names = new HashSet < string > {  
  10.                 "Rajeev",  
  11.                 "Akash",  
  12.                 "Amit"  
  13.             };  
  14.             foreach(var name in names) {  
  15.                 Console.WriteLine(name);  
  16.             }  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  
  20. }  
output

In the code, given above, we are creating a simple HashSet of the string type and adding the strings to it. We can also add the string, using the Add method. We will see how we can use the Add method in the snippet, given below. We will now try to add the duplicate string and see what happens.
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Text;  
  4. using System.Threading.Tasks;  
  5. namespace HashSetDemo {  
  6.     class Program {  
  7.         static void Main(string[] args) {  
  8.             HashSet < string > names = new HashSet < string > {  
  9.                 "Rajeev",  
  10.                 "Akash",  
  11.                 "Amit"  
  12.             };  
  13.             names.Add("Rajeev");  
  14.             //duplicates are not added into collection.   
  15.             foreach(var name in names) {  
  16.                 Console.WriteLine(name);  
  17.             }  
  18.             Console.ReadKey();  
  19.         }  
  20.     }  
  21. }  
output

In the snippet, given above, even though we try to add a duplicate string, we will not get any error but when we iterate the collection, we cannot find the string. This shows that we cannot add the duplicate elements to a HashSet. Now, we will look into some of the important methods of HashSet.  
 
UnionWith


This method combines the elements, present in both the collections into the collection on which it is called.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace HashSetDemo {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             HashSet < string > names = new HashSet < string > {  
  10.                 "Rajeev",  
  11.                 "Akash",  
  12.                 "Amit"  
  13.             };  
  14.             HashSet < string > names1 = new HashSet < string > {  
  15.                 "Rajeev",  
  16.                 "Akash",  
  17.                 "Amit",  
  18.                 "Deepak",  
  19.                 "Mohit"  
  20.             };  
  21.             names.UnionWith(names1);  
  22.             foreach(var name in names) {  
  23.                 Console.WriteLine(name);  
  24.             }  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }  
output
 
IntersectWith

This method combines the elements that are common to both the collections.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace HashSetDemo {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             HashSet < string > names = new HashSet < string > {  
  10.                 "Rajeev",  
  11.                 "Akash",  
  12.                 "Amit"  
  13.             };  
  14.             HashSet < string > names1 = new HashSet < string > {  
  15.                 "Rajeev",  
  16.                 "Akash",  
  17.                 "Amit",  
  18.                 "Deepak",  
  19.                 "Mohit"  
  20.             };  
  21.             names.IntersectWith(names1);  
  22.             foreach(var name in names) {  
  23.                 Console.WriteLine(name);  
  24.             }  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }  
output
 
ExceptWith

This method removes all the elements that are present in the other collections from the collection on which it is called.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace HashSetDemo {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             HashSet < string > names = new HashSet < string > {  
  10.                 "Rajeev",  
  11.                 "Akash",  
  12.                 "Amit"  
  13.             };  
  14.             HashSet < string > names1 = new HashSet < string > {  
  15.                 "Rajeev",  
  16.                 "Akash",  
  17.                 "Amit",  
  18.                 "Deepak",  
  19.                 "Mohit"  
  20.             };  
  21.             names1.ExceptWith(names);  
  22.             foreach(var name in names1) {  
  23.                 Console.WriteLine(name);  
  24.             }  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }  

output

Now, let's go a step further, create a class and try to create a HashSet of the class type and add try to add the duplicates to it.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace HashSetDemo {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             Console.WriteLine("-----Custom HashSet With Duplicates----");  
  10.             HashSet < Employee > employees = new HashSet < Employee > {  
  11.                 {  
  12.                     new Employee {  
  13.                         Emp_Id = 1, Emp_name = "Rajeev", Dept_name = "IT"  
  14.                     }  
  15.                 },  
  16.                 {  
  17.                     new Employee {  
  18.                         Emp_Id = 1, Emp_name = "Rajeev", Dept_name = "IT"  
  19.                     }  
  20.                 },  
  21.                 {  
  22.                     new Employee {  
  23.                         Emp_Id = 3, Emp_name = "Akash", Dept_name = "IT"  
  24.                     }  
  25.                 },  
  26.                 {  
  27.                     new Employee {  
  28.                         Emp_Id = 4, Emp_name = "Amit", Dept_name = "IT"  
  29.                     }  
  30.                 }  
  31.             };  
  32.             Console.WriteLine("{0,-6}{1,10}{2,-8}""Emp_Id""Emp_name""Dept_name");  
  33.             Console.WriteLine("==============================");  
  34.             foreach(var employee in employees) {  
  35.                 Console.WriteLine("{0,-8}{1,-10}{2,5}", employee.Emp_Id, employee.Emp_name, employee.Dept_name);  
  36.             }  
  37.             Console.WriteLine("==============================");  
  38.             Console.ReadKey();  
  39.         }  
  40.     }  
  41.     public class Employee {  
  42.         public int Emp_Id {  
  43.             get;  
  44.             set;  
  45.         }  
  46.         public string Emp_name {  
  47.             get;  
  48.             set;  
  49.         }  
  50.         public string Dept_name {  
  51.             get;  
  52.             set;  
  53.         }  
  54.     }  
  55. }  
output

We know that HashSet will not allow the duplicates into the collection but still in the output we are having the duplicate records. To overcome this drawback, we need to implement IEquatable interface, override Equals and GetHashCode methods.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace HashSetDemo {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             Console.WriteLine("-----Custom HashSet With Duplicates----");  
  10.             HashSet < Employee > employees = new HashSet < Employee > {  
  11.                 {  
  12.                     new Employee {  
  13.                         Emp_Id = 1, Emp_name = "Rajeev", Dept_name = "IT"  
  14.                     }  
  15.                 },  
  16.                 {  
  17.                     new Employee {  
  18.                         Emp_Id = 1, Emp_name = "Rajeev", Dept_name = "IT"  
  19.                     }  
  20.                 },  
  21.                 {  
  22.                     new Employee {  
  23.                         Emp_Id = 3, Emp_name = "Akash", Dept_name = "IT"  
  24.                     }  
  25.                 },  
  26.                 {  
  27.                     new Employee {  
  28.                         Emp_Id = 4, Emp_name = "Amit", Dept_name = "IT"  
  29.                     }  
  30.                 }  
  31.             };  
  32.             Console.WriteLine("{0,-6}{1,10}{2,-8}""Emp_Id""Emp_name""Dept_name");  
  33.             Console.WriteLine("==============================");  
  34.             foreach(var employee in employees) {  
  35.                 Console.WriteLine("{0,-8}{1,-10}{2,5}", employee.Emp_Id, employee.Emp_name, employee.Dept_name);  
  36.             }  
  37.             Console.WriteLine("==============================");  
  38.             Console.ReadKey();  
  39.         }  
  40.     }  
  41.     public class Employee: IEquatable < Employee > {  
  42.         public int Emp_Id {  
  43.             get;  
  44.             set;  
  45.         }  
  46.         public string Emp_name {  
  47.             get;  
  48.             set;  
  49.         }  
  50.         public string Dept_name {  
  51.             get;  
  52.             set;  
  53.         }  
  54.         public bool Equals(Employee other) {  
  55.             return this.Emp_Id.Equals(other.Emp_Id);  
  56.         }  
  57.         public override int GetHashCode() {  
  58.             return this.Emp_Id.GetHashCode();  
  59.         }  
  60.     }  
  61. }  
output

Thus, HashSet is a generic collection, that does not allow duplicates. We can use HashSet to remove the duplicates from any collection like the List, using HashSet. For viewing the code, go to the following link.