What is HashSet in C#: Definition and Usage
HashSet<T> in C# is a collection that stores unique elements without any particular order. It is optimized for fast lookups, additions, and removals, making it ideal when you need to ensure no duplicates exist in your data.How It Works
Think of a HashSet<T> like a basket where you can put items, but the basket automatically prevents you from adding the same item twice. It uses a process called hashing, which transforms each item into a special code to quickly check if it is already inside.
This means when you add or check for an item, the HashSet doesn’t have to look through every element one by one. Instead, it uses the hash code to jump directly to where the item should be, making operations very fast even with many items.
Unlike lists, HashSet does not keep items in the order you add them. Its main goal is to keep elements unique and provide quick access.
Example
This example shows how to create a HashSet<string>, add items, and see that duplicates are ignored.
using System; using System.Collections.Generic; class Program { static void Main() { HashSet<string> fruits = new HashSet<string>(); fruits.Add("apple"); fruits.Add("banana"); fruits.Add("apple"); // duplicate, will be ignored foreach (string fruit in fruits) { Console.WriteLine(fruit); } } }
When to Use
Use a HashSet<T> when you need to store a collection of unique items and want fast checks to see if an item exists. It is perfect for removing duplicates from data or quickly testing membership.
For example, if you have a list of email addresses and want to ensure each address is only stored once, a HashSet is a great choice. It is also useful in scenarios like tracking visited nodes in a graph or managing unique tags in a system.
Key Points
- Unique elements: Automatically prevents duplicates.
- Fast operations: Adds, removes, and checks are very quick.
- No order: Does not keep elements in insertion order.
- Uses hashing: Relies on hash codes for performance.