When working with data structures and algorithms, one concept that keeps popping up is hashing. From implementing efficient search operations to building real-world applications like password storage, caching, and indexing in databases, hashing plays a crucial role.
In this guide, we’ll break down what hashing is, why it’s important, the different hashing techniques, and practical examples. By the end, you’ll have a crystal-clear understanding of how hashing works and where you can apply it in real life.

Hashing is a technique to uniquely identify or map data of arbitrary size into fixed-size values, often called a hash value or hash code.
Think of it as assigning a “digital fingerprint” to your data.
For example:
Even though the data size (length of names) is different, the hash function maps them into a fixed range.
Hashing is widely used in computer science because it allows for:
1. Fast Data Retrieval → Quick lookup in hash tables (average O(1) time complexity).
2. Efficient Indexing → Used in databases for faster searching.
3. Secure Storage → Passwords are stored as hashes, not plain text.
4. Data Integrity → Hashing verifies data hasn’t been tampered with (e.g., in file transfers).
5. Cryptography → Essential in algorithms like SHA (Secure Hash Algorithm) and MD5.
A hash function is the heart of hashing. It takes input data and generates a fixed-size hash code.
1. Deterministic → Same input always gives the same output.
2. Fast Computation → Should be quick to calculate.
3. Uniform Distribution → Avoids clustering of values.
4. Collision Resistant → Minimizes cases where two inputs give the same hash.
5. Irreversible (in cryptography) → Hard to reverse-engineer the original data.
Let’s explore different hashing methods with examples.
1. Division Method
This is the simplest method.
Formula:
h(k) = k % m
Where:
Example:
Suppose we have keys: 10, 22, 31, 4, and a hash table of size 7.
10 % 7 = 3
22 % 7 = 1
31 % 7 = 3
4 % 7 = 4
Here, 10 and 31 collide since both map to index 3.
2. Multiplication Method
Formula:
h(k) = floor(m * (k * A % 1))
Where A is a constant (0 < A < 1).
This method reduces clustering compared to division.
3. Mid-Square Method
Square the key and extract middle digits.
Example:
Key = 123 → Square = 15129 → Middle digits = 12 → Index = 12.
4. Folding Method
Break the key into parts and add them together.
Example:
Key = 123456 → Split into 12, 34, 56 → Sum = 102 → Index = 102.
5. Double Hashing
Uses two hash functions to reduce collisions.
h(k) = (h1(k) + i * h2(k)) % m
where i is the attempt number.
Collision Handling in Hashing
A collision happens when two keys map to the same hash index. This is unavoidable, but we can handle it.
1. Chaining
Example:
Index 3 → [10 → 31]
2. Open Addressing
Types:
1. Password Protection → Store hashed passwords.
2. Data Structures → Implement hash tables and sets.
3. Cryptography → Digital signatures, blockchain, etc.
4. Databases → Hash indexing for quick access.
5. File Systems → Hashing helps in data retrieval.
Copy Code
#include <iostream>
#include <list>
using namespace std;
class HashTable {
int BUCKET;
list<int>* table;
public:
HashTable(int V) {
BUCKET = V;
table = new list<int>[BUCKET];
}
int hashFunction(int x) {
return (x % BUCKET);
}
void insertItem(int key) {
int index = hashFunction(key);
table[index].push_back(key);
}
void displayHash() {
for (int i = 0; i < BUCKET; i++) {
cout << i;
for (auto x : table[i])
cout << " --> " << x;
cout << endl;
}
}
};
int main() {
HashTable h(7);
int arr[] = {10, 22, 31, 4, 15, 28};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++)
h.insertItem(arr[i]);
h.displayHash();
return 0;
}Output:
0
1 --> 22
2 --> 15
3 --> 10 --> 31
4 --> 4
5 -->
6 --> 28
Q1. What is hashing in simple words?
Hashing is a process of converting data into a fixed-size code (hash value) using a hash function.
Q2. What are the main applications of hashing?
Hashing is used in password protection, cryptography, database indexing, file systems, and caching.
Q3. How do you handle collisions in hashing?
Using chaining (linked lists) or open addressing methods like linear probing, quadratic probing, and double hashing.
Q4. Is hashing better than searching with arrays?
Yes, because hashing provides O(1) average search time compared to O(n) in linear search.
Q5. What is the difference between hashing and encryption?
Hashing is one of the most powerful and practical techniques in computer science. Whether you’re working with data structures, building a database, or implementing a secure login system, hashing will be your go-to tool.
The key is to choose a good hash function and apply the right collision handling technique. Once you master hashing, many advanced concepts like cryptography, blockchain, and distributed systems will become easier to understand.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR