Initializing HashSet in C#

A HashSet in C# is a collection that stores unique elements and automatically eliminates duplicates. It provides fast lookups and is ideal when you need to ensure all elements are distinct.

Syntax

Following is the syntax for initializing a HashSet −

var hashSet = new HashSet<T>();
var hashSet = new HashSet<T>(collection);
var hashSet = new HashSet<T> { element1, element2, element3 };

Using Array to Initialize HashSet

You can initialize a HashSet with an existing array. The HashSet will automatically remove any duplicate values from the array −

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

class Program {
   static void Main() {
      string[] arr1 = {
         "electronics",
         "accessories",
         "electronics",
      };

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

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

The output of the above code is −

Original array: electronics,accessories,electronics
HashSet result: electronics,accessories

Using Collection Initializer

You can also initialize a HashSet using collection initializer syntax −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      var numbers = new HashSet<int> { 1, 2, 3, 2, 4, 1, 5 };
      
      Console.WriteLine("HashSet contains:");
      foreach (int num in numbers) {
         Console.WriteLine(num);
      }
      Console.WriteLine("Total unique elements: " + numbers.Count);
   }
}

The output of the above code is −

HashSet contains:
1
2
3
4
5
Total unique elements: 5

Using Empty HashSet and Adding Elements

You can create an empty HashSet and add elements using the Add() method −

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      var fruits = new HashSet<string>();
      
      fruits.Add("apple");
      fruits.Add("banana");
      fruits.Add("apple"); // duplicate - will not be added
      fruits.Add("orange");
      
      Console.WriteLine("Fruits in HashSet:");
      foreach (string fruit in fruits) {
         Console.WriteLine(fruit);
      }
      Console.WriteLine("Total fruits: " + fruits.Count);
   }
}

The output of the above code is −

Fruits in HashSet:
apple
banana
orange
Total fruits: 3

Key Benefits of HashSet

Feature Description
Unique Elements Automatically eliminates duplicate values
Fast Lookups O(1) average time complexity for search operations
Set Operations Supports Union, Intersection, and other set operations
No Indexing Elements are not accessed by index, only by value

Conclusion

HashSet in C# is initialized using constructors or collection initializers and automatically maintains unique elements. It's perfect for scenarios where you need to eliminate duplicates and perform fast membership testing, making it more efficient than List for uniqueness operations.

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

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements