How does a hash table work?
A hash table works by taking a piece of data (called a key), running it through a mathematical formula (called a hash function), and using the result to find a specific spot in an array to store or find the associated value.
Think of it like a coat check at a museum. You give the attendant your coat, and they give you a ticket with a number. When you return, you don't have to search through every coat; you just hand them the number, and they go directly to that specific hook. In a hash table, the key is the coat, the hash function is the ticket generator, and the array indexes are the hooks.
The Hash Function
The engine of a hash table is the hash function. Its job is to take an input of any size (like a person's name or a string of text) and return a fixed-size integer. This integer is then used as an index in an underlying array. For example, if you input the word 'Apple', the hash function might crunch the letters and spit out the number 4. A crucial rule is that the same input must always produce the same output. If 'Apple' gives you 4 today, it must give you 4 tomorrow, otherwise you would never find your data again.
Buckets and the Underlying Array
Behind the scenes, a hash table is basically just an array. Each slot in this array is often called a 'bucket'. When you want to store a key-value pair, the hash table calculates the hash of the key, uses the modulo operator () to make sure the number fits within the array's bounds, and places the data in that bucket. Because arrays allow for immediate access if you know the index, finding data in a hash table takes time on average. You just compute the hash and look in that exact bucket.
Dealing with Collisions
A common place students slip up is forgetting about collisions. Because the array has a fixed size and the number of possible keys is infinite, two different keys will eventually produce the same index. This is called a collision. There are two main ways to handle this. The first is 'Chaining', where each bucket holds a linked list of entries; if two items go to the same bucket, they just join the list. The second is 'Open Addressing', where the hash table looks for the next available empty bucket to store the colliding item.
Worked through
Imagine an empty hash table with an array size of 5 (indexes 0 to 4). We want to insert two keys: 'Dog' (value: 40) and 'Cat' (value: 20). Assume our hash function assigns 'Dog' a raw hash of 14 and 'Cat' a raw hash of 24. We use Chaining for collisions. Show where they end up.
First, we calculate the index for 'Dog'. We take the raw hash modulo the array size: . So, the pair ('Dog', 40) is stored in bucket 4.
Next, we calculate the index for 'Cat'. We take its raw hash modulo the array size: . This is a collision! Because both keys map to bucket 4, we use chaining. Bucket 4 will now contain a linked list with two nodes: ('Dog', 40) and ('Cat', 20).
The final table looks like this: Bucket 0: empty Bucket 1: empty Bucket 2: empty Bucket 3: empty Bucket 4: ('Dog', 40) -> ('Cat', 20)
Questions students ask
Ask about this topic
Where this comes from: Introduction to Algorithms by Thomas H. Cormen et al. · OpenStax: Data Structures and Algorithms · Grokking Algorithms by Aditya Bhargava
See also