HashSet vs List in C#: Key Differences and When to Use Each
HashSet is a collection that stores unique elements with fast lookup and no guaranteed order, while List allows duplicates, maintains insertion order, and supports indexed access. Use HashSet when you need quick membership tests and uniqueness, and List when order and duplicates matter.Quick Comparison
Here is a quick side-by-side comparison of HashSet and List in C# based on key factors.
| Factor | HashSet | List |
|---|---|---|
| Duplicates Allowed | No, only unique elements | Yes, duplicates allowed |
| Order Maintained | No guaranteed order | Yes, maintains insertion order |
| Lookup Speed | Fast (O(1) average) | Slower (O(n)) |
| Index Access | No index access | Supports index access |
| Use Case | Fast membership and uniqueness | Ordered collection with duplicates |
| Memory Usage | Higher due to hashing | Lower compared to HashSet |
Key Differences
HashSet is designed to store unique elements only. It uses a hash-based structure internally, which allows very fast checks to see if an item exists. This makes it ideal when you want to avoid duplicates and quickly test membership. However, it does not keep elements in any particular order, so you cannot rely on the order of items when iterating.
On the other hand, List is a simple ordered collection that allows duplicates. It stores elements in the order they were added and supports accessing elements by their index. Lookup operations like checking if an item exists are slower because it may need to scan through the list. Lists use less memory than hash sets because they don't store extra data for hashing.
In summary, HashSet is best when uniqueness and fast lookup are priorities, while List is better when order and duplicates matter or when you need to access elements by position.
Code Comparison
Here is an example showing how to add elements and check for existence using a HashSet in C#.
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 ignored Console.WriteLine("Fruits in HashSet:"); foreach (var fruit in fruits) { Console.WriteLine(fruit); } Console.WriteLine("Contains 'banana'? " + fruits.Contains("banana")); } }
List Equivalent
Here is the equivalent example using a List in C# to add elements and check for existence.
using System; using System.Collections.Generic; class Program { static void Main() { List<string> fruits = new List<string>(); fruits.Add("apple"); fruits.Add("banana"); fruits.Add("apple"); // Duplicate allowed Console.WriteLine("Fruits in List:"); foreach (var fruit in fruits) { Console.WriteLine(fruit); } Console.WriteLine("Contains 'banana'? " + fruits.Contains("banana")); } }
When to Use Which
Choose HashSet when you need to store unique items and perform fast membership checks without caring about order. It is perfect for scenarios like removing duplicates or quickly testing if an item exists.
Choose List when you need to preserve the order of elements, allow duplicates, or access items by their position. Lists are better for ordered collections, queues, or when you want to keep all entries including duplicates.
In short, use HashSet for uniqueness and speed, and List for order and duplicates.