Hashing is a powerful technique used to store and retrieve data efficiently. It converts input data (called a key) into a fixed-size value known as a hash code or hash value, which determines the index where the data is stored in a table called a hash table.
Hashing is a process of mapping data of arbitrary size to fixed-size values using a hash function. It provides fast data access in constant time on average, making it ideal for searching, insertion, and deletion operations.
// Example (in C++)
int hashFunction(int key) {
return key % 10; // returns index between 0–9
}
Here, each key is converted into an index by taking its remainder when divided by 10. The resulting index is used to store the data in the hash table.
A hash table is a data structure that stores key–value pairs. It uses a hash function to compute the index for storing and retrieving values quickly.
A hash function is a function that takes a key as input and returns an index in the hash table. It should distribute keys uniformly to reduce collisions.
A collision occurs when two keys produce the same hash index. Since multiple keys cannot occupy the same slot, a collision resolution technique is required.
| Key | Hash Function (key % 10) | Index |
|---|---|---|
| 25 | 25 % 10 = 5 | 5 |
| 37 | 37 % 10 = 7 | 7 |
| 47 | 47 % 10 = 7 (collision) | 7 → handle using chaining |
Hashing and hash tables offer one of the most efficient methods for data storage and retrieval. By using an effective hash function and proper collision handling, they ensure fast and scalable performance in real-world applications.
Take quizzes related to this topic and see where you stand!
Start Quiz Now