Search⌘ K
AI Features

Sets and Performance Complexity

Explore the key differences between List and Set collections in C#. Understand how HashSet<T> ensures uniqueness and delivers constant time lookups. Learn about Big O performance complexities and how to use set operations like intersection, union, and differences. This lesson helps you evaluate when to use sorted or hash-based collections to optimize application performance.

While List<T> is ideal for maintaining a specific sequence of items, it allows duplicate values, and requires a sequential search to find an element. A set is a collection that guarantees uniqueness; it contains no duplicate elements.

The HashSet collection

In .NET, the primary implementation is HashSet<T>. It is specifically optimized for high-speed lookups and mathematical set operations. You would typically use a HashSet<T> in scenarios such as tracking unique website visitor IDs, managing distinct product tags, or filtering duplicates from a large dataset.

Key characteristics of sets

  • Guaranteed uniqueness: A set automatically prevents the insertion of duplicate items.

  • No indexing: Unlike a list, you cannot access a set element by a numeric index like mySet[0]. The items are not stored in a guaranteed sequence.

  • High performance: It uses hashing algorithms to achieve near-instantaneous search speeds regardless of the collection size.

Algorithmic complexity (Big O basics)

We use Big O notation to evaluate and compare algorithmic efficiency. This notation measures how the execution time of an algorithm grows as the size of the dataset increases. ...