Hash Sets Resource - aloalgo

Hash Sets

A hash set stores unique elements with O(1) average time for add, remove, and lookup operations. Use it when you only care about existence, not associated values.

Basic Operations

Set Operations

Common Use Cases

Detecting Duplicates

Finding Intersection

Tracking Visited States

Finding Missing/Extra Elements

Longest Consecutive Sequence

A classic set problem: find the length of the longest consecutive sequence in O(n) time.

Time Complexity Reference

OperationAverageWorst
AddO(1)O(n)*
RemoveO(1)O(n)*
Lookup (in)O(1)O(n)*
IterationO(n)O(n)
UnionO(m + n)O(m + n)
IntersectionO(min(m, n))O(m * n)*
*Worst case O(n) occurs with hash collisions. In practice with good hash functions, this is rare.

Common Interview Tips

  1. O(1) membership testing: Replace O(n) list lookups with O(1) set lookups when checking if an element exists.
  2. Sequence start trick: For consecutive sequence problems, only start counting from elements where n-1 is not in the set.
  3. Deduplication: Converting a list to a set instantly removes all duplicates.
Was this helpful?
© 2026 aloalgo. All rights reserved.