DSA Hash Maps
DSA Hash Maps – Complete Beginner Guide
(Concept • Operations • Internal Working • Examples • Implementation • Complexity)
A Hash Map (also called Hash Table / Dictionary) is one of the most powerful data structures in DSA.
It stores data in key–value pairs and provides very fast access.
1️⃣ What is a Hash Map?
A Hash Map stores data as:
-
Key: Unique identifier
-
Value: Data associated with the key
-
Hash Function: Converts key into an index
Example
📌 Keys must be unique, values can repeat.
2️⃣ Why Use Hash Maps?
✔ Very fast lookup
✔ Efficient insert & delete
✔ Direct access using keys
✔ Ideal for large datasets
📌 Average time complexity is O(1).
3️⃣ How Hash Map Works Internally
-
Key is passed to a hash function
-
Hash function returns an index
-
Value is stored at that index
-
If two keys get same index → collision
4️⃣ Hash Function
A good hash function:
-
Is fast
-
Distributes keys uniformly
-
Minimizes collisions
Example
5️⃣ What is Collision?
A collision happens when:
Example:
6️⃣ Collision Resolution Techniques
1. Chaining
-
Each index stores a linked list
-
All collided elements go into that list
✔ Simple
❌ Extra memory
2. Open Addressing
Find another empty slot.
Types:
-
Linear Probing
-
Quadratic Probing
-
Double Hashing
✔ Memory efficient
❌ Clustering issues
7️⃣ Basic Hash Map Operations
| Operation | Description |
|---|---|
| Insert | Add key–value pair |
| Search | Get value using key |
| Update | Modify value |
| Delete | Remove key |
8️⃣ Hash Map Example (C++ – STL)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Using unordered_map: #include <iostream> #include <unordered_map> using namespace std; int main() { unordered_map<string, string> capital; capital["India"] = "New Delhi"; capital["USA"] = "Washington"; capital["France"] = "Paris"; cout << capital["India"] << endl; for(auto it : capital) { cout << it.first << " → " << it.second << endl; } return 0; } |
Output (order may vary)
9️⃣ Frequency Count Using Hash Map
( Very Common Interview Question)
🔟 Hash Map Implementation Idea (Conceptual)
📌 Real implementations also handle resizing & rehashing.
1️⃣1️⃣ Load Factor
-
High load factor → more collisions
-
Hash maps resize automatically when load factor exceeds limit
1️⃣2️⃣ Time Complexity
| Operation | Average | Worst Case |
|---|---|---|
| Insert | O(1) | O(n) |
| Search | O(1) | O(n) |
| Delete | O(1) | O(n) |
📌 Worst case occurs due to heavy collisions.
1️⃣3️⃣ Space Complexity
-
O(n) for storing elements
-
Extra space for buckets / chains
1️⃣4️⃣ Hash Map vs Hash Set
| Feature | Hash Map | Hash Set |
|---|---|---|
| Stores | Key–Value | Values only |
| Duplicates | Keys ❌ | ❌ |
| Use Case | Mapping | Uniqueness |
| Example | Phonebook | Unique IDs |
1️⃣5️⃣ Real-Life Uses
✔ Phone directory
✔ Caching systems
✔ Database indexing
✔ Counting frequency
✔ Two-sum problems
Interview Questions (Hash Map)
Q1. What is a hash map?
👉 Key–value based data structure
Q2. Why hash map is fast?
👉 Direct indexing via hash function
Q3. What causes worst-case O(n)?
👉 Too many collisions
Q4. Difference between map & unordered_map?
👉 Ordered vs unordered
Final Summary
✔ Hash Maps store key–value pairs
✔ Use hashing for fast access
✔ Average O(1) operations
✔ Collisions handled internally
✔ Extremely important for DSA & interviews
