What is a hash table?
A hash table stores keys in buckets. A hash function turns each key into a bucket number, so a lookup can jump almost directly to the right place instead of scanning everything.
Different keys can land in the same bucket, which is called a collision. This visualizer uses chaining, where colliding keys are linked together inside the bucket. The load factor, keys divided by buckets, shows how crowded the table is.
Time complexity
| Operation | Time | Notes |
|---|---|---|
| Search, insert, delete | O(1) average | The hash picks the bucket directly |
| Search, insert, delete (worst case) | O(n) | Every key landed in one bucket |
| Resize and rehash | O(n) | Every key is placed again |
Try it yourself
- Insert numbers and words and read the hash calculation in the Output box.
- Keep adding keys until two land in the same bucket and form a chain.
- Change Buckets to see all keys rehashed.
- Search a key to see the bucket highlight, then its chain.
Hash table vs binary search tree
A hash table gives O(1) average lookup but no ordering. If you need sorted keys or range queries, use a balanced tree instead.
Common questions
- What is a hash collision?
- A collision happens when two different keys hash to the same bucket.
- What is the load factor?
- The number of stored keys divided by the number of buckets. A high load factor means longer chains and slower operations.
- Why is hash table lookup O(1) on average?
- The hash function picks the bucket directly, and with a good spread of keys each chain stays short.