Convert an array to a HashSetT in .NET
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting an array to a HashSet<T> in .NET removes duplicates and enables O(1) average-time lookups, adds, and removals. The simplest approach is passing the array directly to the HashSet<T> constructor. You can also use LINQ's ToHashSet() extension method (available since .NET Core 2.0 / .NET Framework 4.7.2). Both approaches produce the same result — a set containing the unique elements from the array.
Constructor Approach
The HashSet<T> constructor accepts any IEnumerable<T>, so arrays, lists, and other collections all work.
ToHashSet() Extension Method
ToHashSet() is often combined with LINQ queries to convert filtered or transformed results into a set:
Custom Equality Comparer
By default, HashSet<T> uses EqualityComparer<T>.Default, which calls GetHashCode() and Equals(). For custom types, you may need to provide your own comparer:
Set Operations
Once you have a HashSet<T>, you can perform efficient set operations:
Performance Comparison
| Operation | Array | HashSet |
| Contains (lookup) | O(n) | O(1) average |
| Add | O(n) if resizing | O(1) average |
| Remove | O(n) | O(1) average |
| Iteration | O(n) | O(n) |
| Memory | Contiguous, compact | Hash buckets, higher overhead |
| Preserves order | Yes | No (unordered) |
| Allows duplicates | Yes | No |
Use HashSet<T> when you need fast membership tests or deduplication. Use arrays when you need indexed access, ordering, or minimal memory footprint.
Converting Back to Array
Common Pitfalls
- Assuming order is preserved:
HashSet<T>does not guarantee insertion order. If you need ordered unique elements, useLinkedHashSet(not built-in) orSortedSet<T>(sorted order). - Forgetting to override
GetHashCodeandEquals: For custom classes, the defaultHashSet<T>uses reference equality. Two different objects with the same data are treated as distinct. ImplementIEqualityComparer<T>or override both methods on the class. - Mutating objects after adding to HashSet: If you change a property that affects
GetHashCode()after inserting the object, the set cannot find it anymore. The hash bucket is wrong, causingContainsto return false and duplicates to be added. - Using
ToHashSet()on older .NET Framework versions:ToHashSet()was added in .NET Framework 4.7.2 and .NET Core 2.0. On older versions, use the constructor:new HashSet<T>(array). - Thread safety:
HashSet<T>is not thread-safe. For concurrent access, useConcurrentDictionary<T, byte>as a workaround or protect access with a lock.
Summary
- Pass an array to
new HashSet<T>(array)or callarray.ToHashSet()to convert - Duplicates are automatically removed during conversion
- Provide an
IEqualityComparer<T>for custom equality logic or case-insensitive comparisons HashSet<T>provides O(1) lookups, adds, and removes — much faster than searching an array- Use set operations (
IntersectWith,UnionWith,ExceptWith) for efficient collection comparisons - Order is not preserved — use
SortedSet<T>if you need sorted output

