A Hash Table (also known as a Hash Map) is a data structure that stores data in key–value pairs, allowing very fast lookup, insertion, and deletion — typically O(1) on average.
It is one of the most important data structures in computer science and is used in databases, compilers, caches, interpreters, browsers, operating systems, and more.
Learn data structures and algorithms online
A hash table works by converting (or hashing) a key into an index, and storing the corresponding value in an array at that index.
"username", "email", 42).index = hash(key)
table[index] = value This allows the value to be found again quickly because the same key will hash to the same index.
Hash tables give O(1) time complexity for:
Because the hash function jumps directly to where data is stored — no linear searching required.
Internally:
A hash function takes a key and returns a number.
Example pseudo-hashing:
hash("cat") → 9
hash("dog") → 2
hash("cow") → 4 A good hash function should:
The hash table uses an array underneath.
Example (size = 10):
| Index | Value |
|---|---|
| 0 | NULL |
| 1 | NULL |
| 2 | “dog” |
| 3 | NULL |
| 4 | “cow” |
| 9 | “cat” |
A collision happens when two keys hash to the same index.
Example:
hash("lion") → 2
hash("dog") → 2 Both want to occupy index 2.
Hash tables must handle collisions gracefully.
There are two major methods.
Each bucket stores a list of items.
Example:
table[2] → ["dog", "lion", ...] Benefits:
Drawback:
If index is occupied, search for the next available slot.
Common strategies:
index = hash(key)
if occupied:
index = index + 1 index + 1^2, 2^2, 3^2, ... Use a second hash function when the first causes a collision.
The ratio:
LF = number_of_elements / table_size If load factor becomes too high (e.g., > 0.7):
Solution: resize (typically double the array size), then rehash all keys.
| Operation | Average Case | Worst Case |
|---|---|---|
| Insert | O(1) | O(n) |
| Search | O(1) | O(n) |
| Delete | O(1) | O(n) |
Worst case occurs when all elements land in one bucket.
But with good hashing and resizing, O(1) dominates.
dictMap, object keysHashMapunordered_maplet table = {};
table["name"] = "Luke";
table["age"] = 30;
console.log(table["name"]); // Luke
table = {}
table["name"] = "Luke"
table["age"] = 30
print(table["age"]) # 30
import java.util.HashMap;
HashMap<String, Integer> map = new HashMap<>();
map.put("age", 30);
System.out.println(map.get("age"));
A Hash Table is a data structure that:
It is one of the foundational concepts every developer must understand.
Latest tech news and coding tips.
React makes building user interfaces easier, but certain errors appear repeatedly, especially when working with…
The C programming language is a foundational, general-purpose computer programming language created by Dennis Ritchie at Bell Labs in 1972. Originally developed to…
What Is Homebrew? Homebrew is a package manager for macOS and Linux that makes it easy to…
JavaScript gives you several ways to iterate over an array. Some are better for simple…
A web server is the software responsible for receiving requests from clients, processing those requests,…
A shell prompt is the text displayed by a command-line shell to show that it is ready…