Open Addressing

Design HashSet

Easy
Solve it on LeetCode ↗

The problem

Implement a hash set (add, remove, contains) for integers in [0, 10⁶] without built-in set types.

Stuck? Reveal hints one at a time

How to approach it

  1. 1Allocate a boolean array of size 10⁶ + 1.
  2. 2add: flag[key] = true. remove: flag[key] = false. contains: return flag[key].

Key insight

Direct addressing is hashing with the identity function — when the key universe is small enough, collisions vanish by construction.

The solution

Watch out for

  • This trades memory for simplicity — mention the bucketed design for unbounded keys in interviews.
  • Uint8Array/bytearray shrink memory 8× versus arrays of booleans/objects.