Back to Benchmark

Hash Map (Hash Table) - O(1) Complexity

Understanding how hash maps provide constant-time lookups and insertions

What is a Hash Map?

A hash map (also called a hash table) is like a smart filing cabinet. Instead of searching through drawers one by one, you use a hash function to calculate exactly which drawer contains what you're looking for!

Example:

const map = new Map();
map.set('name', 'John');
map.set('age', 30);
map.get('name'); // Returns 'John' instantly!

Hash maps store data as key-value pairs. The key is used to find the value quickly using a hash function, which converts the key into an index where the value is stored.

How Does Hashing Work?

1. Hash Function

A hash function takes a key and converts it into a number (hash code). This number determines where in the array the value will be stored.

hash('apple') → 3
hash('banana') → 7
hash('cherry') → 2

2. Index Calculation

The hash code is then converted to an array index using modulo operation:

index = hash_code % array_size

3. Direct Access

Once you know the index, you can access the value directly - just like array random access! This is why hash maps are O(1) for both get and put operations.

Interactive Hash Map Visualization
Click on any key below to see how fast hash map lookups are!

Hash Map Structure (Buckets)

Index 0
apple
10
hash: 0
fig
60
hash: 0
Index 1
empty
Index 2
elderberry
50
hash: 2
Index 3
cherry
30
hash: 3
honeydew
80
hash: 3
Index 4
date
40
hash: 4
Index 5
empty
Index 6
empty
Index 7
banana
20
hash: 7
grape
70
hash: 7
Index 8
empty
Index 9
empty

Key-Value Pairs

Add New Entry

Code Example:

// Creating and using a hash map
const map = new Map();
// O(1) - Insert operation
map.set('apple', 10);
map.set('banana', 20);
// O(1) - Get operation
const value = map.get('apple'); // Returns 10 instantly!
Built-in Functions for Hash Maps and Sets
Most programming languages provide built-in hash map and set data structures with O(1) operations

Why are Hash Map Operations O(1)?

1. Hash Function Calculation

The hash function converts the key to an index in constant time. It doesn't matter how many entries are in the map - the calculation takes the same time.

2. Direct Array Access

Once you have the index, accessing the array at that position is O(1), just like regular array access:

index = hash(key) % size;
value = array[index]; // O(1)

3. No Searching Required

Unlike arrays where you might need to search linearly, hash maps tell you exactly where to look. No iteration needed!

⚠️ Collision Handling

Sometimes two keys hash to the same index (collision). Modern hash maps handle this efficiently using techniques like chaining (linked lists) or open addressing, still maintaining near O(1) performance on average.

Time Complexity Comparison
OperationHash MapArray (Linear Search)Explanation
Get/FindO(1)O(n)Hash map: Direct access via hash. Array: Must check each element.
Insert/PutO(1)O(n)Hash map: Calculate index and insert. Array: May need to shift elements.
DeleteO(1)O(n)Hash map: Find and remove. Array: May need to shift elements.
Iterate AllO(n)O(n)Both require visiting every element once.
Real-World Examples

📞 Phone Book

Like looking up a phone number by name. You don't read through every entry - you use the name (key) to find the number (value) instantly!

🆔 ID Card System

When you show your ID number, the system instantly finds your information. The ID number is the key, your data is the value.

🛒 Shopping Cart

E-commerce sites use hash maps to store cart items. Product ID (key) → Quantity and Price (value). Adding or checking items is instant!

🌐 Web Caching

Browsers cache web pages using URLs (keys) to quickly retrieve cached content (values). This is why revisiting a site is faster!

🔐 User Sessions

Websites track logged-in users using session IDs (keys) mapped to user data (values). Quick lookups enable fast authentication!

Key Takeaways
  • Hash maps provide O(1) average-case operations

    Get, put, and delete operations are constant time on average, making hash maps extremely fast for lookups

  • Hash functions convert keys to indices

    The hash function is the magic that makes direct access possible - it tells you exactly where to look

  • Perfect for key-value lookups

    When you need to find values by keys frequently, hash maps are the best choice

  • Collisions are handled efficiently

    Even when multiple keys hash to the same index, modern implementations handle it well, maintaining near O(1) performance

Practice Exercises
Test your understanding of hash maps with these exercises. Try to solve them yourself before checking the answers!