DSA Hashing
Store and retrieve data quickly using keys, hash functions, and hash tables.
Overview
Hashing is a technique for fast data storage and retrieval. Instead of scanning every value, a hash function determines where a key's data should be stored. This makes hashing valuable for account lookups, caching, databases, duplicate detection, and many interview problems.
Key concepts
- A hash table stores key-value pairs
- A hash function converts a key into a storage index
- Insert, search, and delete are O(1) on average
- Collisions occur when different keys map to the same location
What is Hashing?
Hashing uses a key to store and retrieve information without searching through every element. Think of a library where each book has a shelf number: the number lets you go directly to the correct location.
What is a Hash Table?
A hash table—also called a hash map or dictionary—stores information as key-value pairs.
| Key | Value |
|---|---|
| 101 | Alice |
| 102 | Bob |
| 103 | Charlie |
The keys uniquely identify records, allowing their corresponding values to be retrieved quickly.
What is a Hash Function?
A hash function converts a key into an index. For example, with ten storage positions, the expression 25 % 10 produces index 5.
Key: 25
Hash function: 25 % 10
Index: 5A good hash function is fast and distributes keys evenly across the available positions.
Creating a Hash Map in JavaScript
JavaScript provides Map for storing key-value pairs.
const students = new Map();
students.set(101, "Alice");
students.set(102, "Bob");
students.set(103, "Charlie");
console.log(students.get(102)); // BobThe set method stores a pair, and get retrieves a value using its key.
Common Hash Map operations
Insert
const fruits = new Map();
fruits.set("A", "Apple");Search
console.log(fruits.get("A")); // AppleCheck whether a key exists
console.log(fruits.has("A")); // trueDelete
fruits.delete("A");
console.log(fruits.has("A")); // falsePractical example: frequency counting
A hash map can count values in one traversal, a pattern used in many string and array problems.
function frequencies(values) {
const counts = new Map();
for (const value of values) {
counts.set(value, (counts.get(value) ?? 0) + 1);
}
return counts;
}
console.log(frequencies(["a", "b", "a"]));
// Map { "a" => 2, "b" => 1 }What is a Collision?
A collision occurs when different keys produce the same index.
15 % 10 → Index 5
25 % 10 → Index 5Both keys must still be stored and retrieved correctly, so the hash table needs a collision-resolution strategy.
How are Collisions handled?
Chaining
Chaining stores multiple entries in a bucket using a linked list or another collection.
Index 5 → [15] → [25] → [35]Open Addressing
Open addressing searches the table for another available position instead of storing multiple entries in one bucket.
Time complexity of Hashing
| Operation | Average | Worst case |
|---|---|---|
| Insert | O(1) | O(n) |
| Search | O(1) | O(n) |
| Delete | O(1) | O(n) |
Worst-case behavior can occur when many keys collide. A good hash function, sensible table size, and resizing keep operations efficient in normal use.
Real-life examples
A school system can use a unique student ID as a key to retrieve a record immediately. A website can similarly use a username or email to locate account details without scanning every account.
Advantages of Hashing
- Very fast average search
- Quick insertion and deletion
- Efficient handling of large datasets
- Natural key-value storage
- Broad support in databases and applications
Limitations of Hashing
- Collisions require careful handling
- Poor hash functions reduce performance
- Hash tables do not automatically keep keys sorted
- Buckets and unused capacity consume additional memory
Where is Hashing used?
- Login systems
- Database indexing
- Caching
- Password storage
- Search engines
- URL shorteners
- Session management
- Duplicate detection
Password hashing uses specialized one-way cryptographic functions and salts. It should not be implemented with a general-purpose hash map or a simple modulo function.
Tips for beginners
- Understand key-value pairs
- Practice JavaScript Map operations
- Learn why collisions occur
- Compare hash-table lookup with array and linked-list search
- Practice frequency counting and membership problems
Key takeaways
- Hashing uses keys for fast data access
- Hash tables store key-value pairs
- Hash functions map keys to storage locations
- Core operations average O(1)
- Collisions can use chaining or open addressing
- Hashing supports databases, caching, accounts, sessions, and duplicate detection