C# › Collections
When would you choose a HashSet<T> over a List<T>, and what guarantee does it provide?
IntermediateAnswer
HashSet<T> guarantees every element is UNIQUE (adding a duplicate is a silent no-op) and provides O(1) average .Contains() lookups, unlike List<T>'s O(n) linear .Contains() scan and lack of uniqueness enforcement — ideal when you need fast membership testing or automatic deduplication.
HashSet<int> visitedIds = new();
if (visitedIds.Add(userId)) {
ProcessUser(userId); // only runs the first time this ID is seen
}Real-world example Tracking which user IDs have already been processed in a batch job, avoiding duplicate work efficiently.