← chapter

Sets and multisets

A set: hash table of keys — O(1) membership, no duplicates. A multiset: keys with counts.

Two questions, answered in O(1)

Watch an intersection

Probe each element of A for membership in B. 1,2,3 miss; 4,5,6 hit → A ∩ B = {4,5,6}. Each probe O(1).

Complexity

The seen-set pattern

Record what you've seen → turn O(n²) scans into O(n). The most useful trick in the book.

Takeaway

Membership in O(1) → dedup, "seen before", counting. No order, no duplicates. Next tier: recursion & sorting.