Menu
Coddy logo textTech

Hash Table

Last updated

A hash table stores items in an array of buckets. To place or find a key, it runs the key through a hash function and takes the result modulo the number of buckets - that gives the bucket index in O(1). Press play above to watch each key get hashed to a bucket and stored, then a lookup jump straight to the right bucket.

When two keys hash to the same bucket - a collision - this visualization uses separate chaining: each bucket holds a small list, and colliding keys are appended to it. As long as the table isn't too full (a low load factor) and the hash spreads keys well, chains stay short and insert, lookup, and delete are O(1) on average.

Time complexity

OperationAverageWorst case
InsertO(1)O(n) (all keys collide)
LookupO(1)O(n)
DeleteO(1)O(n)
SpaceO(n)O(n)

Key concepts

TermMeaning
Hash functionMaps a key to a bucket index
CollisionTwo keys land in the same bucket
Separate chainingEach bucket holds a list of colliding entries
Open addressingAlternative: probe for the next free slot
Load factorentries / buckets - drives resize

Worked example

Inserting keys 20, 34, 9, 13 into a table with 7 buckets, using hash(k) = k % 7:

StepStructureAction
Insert 20bucket 6: [20]20 % 7 = 6, bucket 6 empty, store 20
Insert 34bucket 6: [20, 34]34 % 7 = 6, collision with 20, append 34 to the chain
Insert 9bucket 2: [9]9 % 7 = 2, bucket 2 empty, store 9
Insert 13bucket 6: [20, 34, 13]13 % 7 = 6, collision, append 13 to bucket 6's chain
Lookup 34bucket 6: [20, 34, 13]34 % 7 = 6, scan chain: 20 no, 34 match - found

When to use a hash table

Use it whenAvoid it when
You need average O(1) insert, lookup, and delete by keyYou need keys kept in sorted order - use a balanced BST
Keys have no useful ordering and you only test exact-match membershipYou need range queries or nearest-key lookups
You can afford some extra memory for buckets and low load factorMemory is extremely tight and every byte counts
A good hash function exists for your key typeWorst-case latency must be bounded - collisions make it O(n)

Hash Table code

A clean, runnable Hash Table implementation in Python, JavaScript, Java, C++, C. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Hash Table code in Python

Python
1class HashTable:2    def __init__(self, size=8):3        self.size = size4        self.buckets = [[] for _ in range(size)]5
6    def _index(self, key):7        # Hash the key to a bucket; different keys can collide8        return sum(ord(ch) for ch in key) % self.size9
10    def set(self, key, value):11        bucket = self.buckets[self._index(key)]12        for i, (k, _) in enumerate(bucket):13            if k == key:14                bucket[i] = (key, value)  # update existing key15                return16        bucket.append((key, value))  # chain on collision17
18    def get(self, key):19        for k, v in self.buckets[self._index(key)]:20            if k == key:21                return v22        raise KeyError(key)23
24    def delete(self, key):25        bucket = self.buckets[self._index(key)]26        for i, (k, _) in enumerate(bucket):27            if k == key:28                del bucket[i]29                return30        raise KeyError(key)31
32
33table = HashTable()34table.set("apple", 3)35table.set("banana", 7)36table.set("cherry", 5)37
38print("apple  ->", table.get("apple"))39print("banana ->", table.get("banana"))40table.set("apple", 10)41print("apple  ->", table.get("apple"))42table.delete("banana")43print("bucket sizes:", [len(b) for b in table.buckets])
Run this code in the Python Playground

Hash Table FAQ

What is a hash collision and how is it resolved?
A collision happens when two different keys hash to the same bucket. The two common fixes are separate chaining (each bucket holds a list, and colliding keys are appended - shown here) and open addressing (probe for the next empty slot in the array). Both keep lookups correct; they differ in memory layout and performance under high load.
What is the time complexity of a hash table?
Insert, lookup, and delete are O(1) on average when the hash function distributes keys evenly and the load factor is kept low. In the worst case - every key colliding into one bucket - they degrade to O(n), which is why a good hash function and resizing matter.
What is a load factor?
The load factor is the number of stored entries divided by the number of buckets. As it grows, chains get longer and operations slow down, so most hash tables resize (rehash into a bigger array) once it crosses a threshold like 0.75.
When should I use a hash table instead of a binary search tree?
Use a hash table when you only need exact-match lookups and want average O(1) speed with no ordering requirement. Use a balanced binary search tree when you need keys in sorted order, range queries, or predecessor/successor lookups, which a hash table can't do efficiently. A BST gives guaranteed O(log n) operations, while a hash table trades that guarantee for faster average performance.
What is the difference between separate chaining and open addressing?
Separate chaining stores colliding keys in a per-bucket list, so a bucket can hold many entries and the table never truly fills up. Open addressing keeps everything in the array itself and probes for the next free slot on a collision, which is cache-friendly but degrades sharply as the load factor approaches 1 and needs careful deletion handling. Chaining tolerates higher load factors; open addressing uses memory more compactly.
Why can't I rely on a hash table to keep my keys in insertion or sorted order?
A hash function deliberately scatters keys across buckets to avoid clustering, so iteration order reflects bucket layout, not insertion or sort order. If you need ordering, use an ordered map/tree or a structure like an insertion-ordered map (e.g. Python's dict preserves insertion order, but that's a language guarantee, not an inherent hash-table property). Never assume iteration order matches how you inserted keys unless the language explicitly promises it.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED