October 2025
Intermediate to advanced
485 pages
13h 2m
English
Suppose we want to maintain a set of the integers 0, 3, 4, 6. So far, we’ve seen two possible ways of doing this.
We have the hash table approach:
| | {0: True, 3: True, 4: True, 6: True} |
And we also have the Boolean array approach:
| | [True, False, False, True, True, False, True, False] |
But there’s a third approach. What I’m about to show you is one of my favorite tricks in all of computer science. I’ll introduce it by revealing one layer at a time.
To start, let’s take the previous Boolean array and swap out each False for a 0, and each True for a 1:
| | [1, 0, 0, 1, 1, 0, 1, 0] |
Recall that with base 2, we can represent any integer using only 0’s and 1’s. (If you’re rusty with base 2, check out the discussion of binary numbers in Covering ...
Read now
Unlock full access