HashSet in C# is a collection type that stores unique elements and does not allow duplicates. It is an unordered collection optimized for fast lookup, insertion, and removal using hashing. HashSet is ideal when we need to ensure unique data and perform set operations like unions, intersections, and differences efficiently.
List allows duplicate values and performs slower searches, because it checks items one by one.
HashSet, on the other hand, stores only unique values and offers much faster lookups due to its hash-based structure.
What is HashSet in C#
HashSet belongs to the System.Collections.Generic namespace and was introduced in .NET 3.5.
A HashSet<T> is a collection that implements the set data structure concept.
T stands for the type of the elements in the set (e.g.,
int,string, a custom class).It is an unordered collection. Elements are not stored in any particular sequence, and you cannot access them by index.
It enforces uniqueness. If you try to add an element that already exists in the set, the operation is ignored, and the set remains unchanged.
It provides extremely fast performance for operations like adding, removing, and checking for the existence of an element. These operations are typically O(1) constant time.
O(1), or constant time complexity, means that the time it takes to complete an operation is fixed and does not depend on the size of the input. In other words, whether we have a small dataset or a very large one, the operation takes the same amount of time to execute. This is considered highly efficient because the runtime remains constant regardless of data size.For example, accessing an element by index in an array or checking membership in a HashSet typically operates in O(1) time, meaning it performs a fixed number of steps even if the collection grows larger. This is why HashSet methods like Add, Remove, or Contains are said to work in average O(1) time, offering very fast performance for these operations.
How Does it Achieve Uniqueness and Speed?
The Hashset achieves its speed and uniqueness by utilizing a technique called hashing.
When an element is added, the set first calls the object's GetHashCode() method to calculate an integer hash code.
This hash code determines a "bucket" or index where the element should be stored in the underlying data structure (often a specialized form of a hash table).
To ensure uniqueness, the set then checks if an element with the exact same value already exists in that bucket by calling the object's
Equals()method. If both the hash code and the value are equal to an existing element, the insertion is blocked.
This hash-based approach allows the runtime to quickly jump to the correct location in memory, resulting in blazing fast lookups.
HashSet Declaration and Syntax
The Hashset<T> syntax is straightforward and uses the C# Generics feature, where the <T> specifies the type of element the set will hold.
1. The using Directive
Because Hashset<T> is part of the Collections framework, we should always include the following at the top of your C# file:
using System.Collections.Generic;2.Basic Declaration
HashSet<T> variableName = new HashSet<T>();Example
Imagine building a feature for an event management system where we want to keep track of unique attendees' email addresses. Using a list would require extra code to check for duplicates manually. With HashSet, we can add email addresses directly, and it ensures no duplicates exist automatically:
HashSet<string> attendeeEmails = new HashSet<string>();
attendeeEmails.Add("[email protected]");
attendeeEmails.Add("[email protected]");
attendeeEmails.Add("[email protected]"); // This will be ignored, no duplicates allowed
foreach (var email in attendeeEmails)
Console.WriteLine(email);will be:
[email protected]
[email protected]Explanation
The HashSet stores only unique elements.
When adding "[email protected]" the second time, it is ignored because it's already present in the HashSet.
The foreach loop prints each unique email once, hence "[email protected]" appears only one time, followed by "[email protected]".
The order in a HashSet is not guaranteed, so the printed order could vary, but it will only contain the two unique emails added.
This behavior ensures no duplicates exist in the collection.
Common Operations in HashSet
1. Basic Manipulation Operations
| Method | Description | Example | Time Complexity |
|---|---|---|---|
| Add(T item) | Attempts to add a specified element to the set. Returns true if the element was added (it was unique), and false if the element already existed (it was ignored). | mySet.Add("data"); | O(1) |
| Remove(T item) | Removes the specified element from the set. Returns true if the element was successfully found and removed, and false otherwise. | mySet.Remove("old"); | O(1) |
| Contains(T item) | Checks if the specified element exists in the set. Returns true or false. | mySet.Contains("test"); | O(1) (The most efficient lookup) |
| Count | Gets the number of elements currently in the set (a property, not a method). | int count = mySet.Count; | O(1) |
| Clear() | Removes all elements from the HashSet. | mySet.Clear(); | O(n) |

Join the conversation! Your thoughts help the community grow.