HashSet in C#

A HashSet in C# is a high-performance collection that stores unique elements and automatically eliminates duplicates. It provides fast lookups, insertions, and deletions with O(1) average time complexity, making it ideal for scenarios where you need to maintain a collection of distinct values.

HashSet is part of the System.Collections.Generic namespace and implements the ISet<T> interface, providing mathematical set operations like union, intersection, and difference.

Syntax

Following is the syntax for declaring and initializing a HashSet −

HashSet<T> hashSetName = new HashSet<T>();

You can also initialize it with an existing collection −

HashSet<T> hashSetName = new HashSet<T>(collection);

Removing Duplicates from Arrays

One of the most common uses of HashSet is to eliminate duplicate elements from arrays or lists −

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      string[] arr1 = {
         "bus",
         "truck",
         "bus",
         "car",
         "truck"
      };

      Console.WriteLine("Original array:");
      Console.WriteLine(string.Join(",", arr1));

      // HashSet eliminates duplicates automatically
      var h = new HashSet<string>(arr1);
      string[] arr2 = h.ToArray();
      
      Console.WriteLine("After removing duplicates:");
      Console.WriteLine(string.Join(",", arr2));
   }
}

The output of the above code is −

Original array:
bus,truck,bus,car,truck
After removing duplicates:
bus,truck,car

Basic HashSet Operations

Adding and Checking Elements

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      HashSet<int> numbers = new HashSet<int>();
      
      // Adding elements
      numbers.Add(10);
      numbers.Add(20);
      numbers.Add(10); // Duplicate - won't be added
      
      Console.WriteLine("Count: " + numbers.Count);
      Console.WriteLine("Contains 10: " + numbers.Contains(10));
      Console.WriteLine("Contains 30: " + numbers.Contains(30));
      
      // Remove element
      numbers.Remove(10);
      Console.WriteLine("After removing 10, count: " + numbers.Count);
   }
}

The output of the above code is −

Count: 2
Contains 10: True
Contains 30: False
After removing 10, count: 1

Set Operations

HashSet provides mathematical set operations like union, intersection, and difference −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      HashSet<int> set1 = new HashSet<int> { 1, 2, 3, 4 };
      HashSet<int> set2 = new HashSet<int> { 3, 4, 5, 6 };
      
      // Union - combines both sets
      HashSet<int> union = new HashSet<int>(set1);
      union.UnionWith(set2);
      Console.WriteLine("Union: " + string.Join(",", union));
      
      // Intersection - common elements
      HashSet<int> intersection = new HashSet<int>(set1);
      intersection.IntersectWith(set2);
      Console.WriteLine("Intersection: " + string.Join(",", intersection));
      
      // Difference - elements in set1 but not in set2
      HashSet<int> difference = new HashSet<int>(set1);
      difference.ExceptWith(set2);
      Console.WriteLine("Difference: " + string.Join(",", difference));
   }
}

The output of the above code is −

Union: 1,2,3,4,5,6
Intersection: 3,4
Difference: 1,2

HashSet vs List Performance Comparison

Operation HashSet<T> List<T>
Add O(1) average O(1) amortized
Contains/Search O(1) average O(n)
Remove O(1) average O(n)
Duplicates Not allowed Allowed
Ordering No guaranteed order Maintains insertion order

Conclusion

HashSet in C# is an efficient collection for storing unique elements with fast lookup, insertion, and removal operations. It automatically handles duplicate elimination and provides mathematical set operations, making it ideal for scenarios requiring distinct values and set-based computations.

Updated on: 2026-03-17T07:04:35+05:30

935 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements