HashSet vs List in C#: When to Use Each
When it comes to storing and managing data in C#, two commonly used collections are HashSet and List. Both have their unique strengths and are suited to different scenarios. This article will guide you through understanding each collection, their key differences, and when to use one over the other.
Understanding HashSet
A HashSet is a collection that uses a hash table for storage. It's ideal for scenarios where you need to store unique elements and perform operations like adding, removing, and checking for existence quickly. Here are some key features of HashSet:
- Does not allow duplicate elements.
- Provides constant time performance for add, remove, and contains operations.
- Does not maintain the insertion order.
Understanding List
A List, on the other hand, is an ordered collection derived from the IEnumerable interface. It allows duplicate elements and maintains the insertion order. Here are some key features of List:

- Allows duplicate elements.
- Maintains the insertion order.
- Provides indexed access to elements.
When to Use HashSet
Given the features of HashSet, it's best suited for the following scenarios:
- Unique Elements: When you need to store unique elements and don't want duplicates, use HashSet.
- Quick Lookups: If you frequently need to check if an element exists in the collection, HashSet's constant time performance for contains operations makes it a better choice.
- No Order Required: If the order of elements does not matter, using HashSet can improve performance.
When to Use List
List is more suitable for the following scenarios:
- Duplicate Elements: When you need to store elements that may have duplicates, use List.
- Ordered Collection: If the order of elements matters, List maintains the insertion order.
- Indexed Access: If you need to access elements by their index, List provides this functionality.
Performance Comparison
Here's a simple comparison of the time complexity for common operations:

| Operation | HashSet | List |
|---|---|---|
| Add | O(1) average, O(n) worst case | O(1) amortized, O(n) worst case |
| Remove | O(1) average, O(n) worst case | O(n) average and worst case |
| Contains | O(1) average, O(n) worst case | O(n) average and worst case |
As you can see, HashSet generally outperforms List in terms of time complexity for add, remove, and contains operations. However, List's indexed access provides a unique advantage.
Example Usage
Here's a simple example demonstrating the use of HashSet and List:
HashSet<string> uniqueWords = new HashSet<string> {};
List<string> allWords = new List<string> {};
// Adding words
uniqueWords.Add("Hello");
uniqueWords.Add("World");
allWords.Add("Hello");
allWords.Add("World");
allWords.Add("Hello"); // Duplicates are allowed in List
// Checking existence
Console.WriteLine(uniqueWords.Contains("Hello")); // True
Console.WriteLine(allWords.Contains("Hello")); // True
// Removing words
uniqueWords.Remove("World");
allWords.RemoveAt(1); // Removes the word at index 1
In this example, HashSet is used to store unique words, while List is used to store all words, including duplicates. The choice between HashSet and List depends on the specific requirements of your use case.























