Hash sets
A collection that answers one question fast: is this item in here? Seven steps: hashing keys into buckets, testing membership, and what uniqueness lets you do.
In 30 seconds
A hash set stores a collection of distinct items, called keys. It is a hash table without the values: a hash function turns each key into a bucket number, and the key is kept in that bucket.
To ask “is x in the set?”, hash x and look in one bucket. That takes about the same time however large the set is.
Since a key can only be stored once, sets are the usual way to remove duplicates from a list and to combine collections: union (in either) and intersection (in both).
Hash sets, step by step
The letter-sum hash is a toy: real hash functions spread keys far more evenly. O(1) is the average case; a real set also enlarges its bucket array as it fills, to keep the chains short.
- Step 1: A table of keys, no values. A hash set is a hash table that stores only keys, with no values attached. A hash function turns each key into a number, and that number picks one of the buckets. Here the toy hash adds up the letters of “fig” and takes the remainder mod 8, so fig goes into bucket 6.
- Step 2: Add more keys. Four more keys are hashed and dropped into their buckets. plum lands in bucket 6, where fig already is: a collision. That is normal. In this design the bucket simply holds a short chain of keys. The set has no order of its own; the keys sit wherever their hashes send them.
- Step 3: Is it in the set?. To test membership, hash the key and look in that one bucket. pear hashes to bucket 0 and is there: true. date hashes to bucket 6, which holds only fig and plum: false. No other bucket is opened, so the cost stays about the same whether the set has five keys or five million.
- Step 4: No duplicates. Adding always starts with the same lookup. fig hashes to bucket 6 again, the set finds fig already there, and nothing is stored. So every key appears at most once: a set records whether something is present, never how many times it was added.
- Step 5: Removing duplicates. That rule makes a set the standard tool for removing repeats. Walk the list once and ask the set “seen this before?” for each item. kiwi, fig and pear are new the first time and kept; every repeat is dropped. Six items need six quick lookups, instead of comparing every item with every other.
- Step 6: Union: in either set. The union of two sets holds every key that is in at least one of them. Build it by copying A and adding each key of B. The uniqueness rule does the rest: kiwi and pear are in both, but appear once in the result, which has 5 + 4 − 2 = 7 keys.
- Step 7: Intersection: in both. The intersection holds only the keys that are in both sets. Loop over the smaller set, B, and ask A whether it contains each key: pear yes, lime no, kiwi yes, date no. Four fast lookups give {kiwi, pear}. The same idea, keeping the keys A does not contain, gives a difference.
Try it yourself
Learn this first
Spotted a mistake on this page?