⚡ What If Searching One by One Is Too Slow?
At the end of Part 1, we imagined an application with:
1,000,000 users.
If those users are stored in a normal list, finding one might mean checking:
User 1 User 2 User 3 ... User 999,999 User 1,000,000
That works.
But it doesn't feel very clever.
What if we already know the user's ID?
user_10592
Wouldn't it be better if we could use that ID to jump almost directly to the correct information?
Sometimes the problem isn't storing information. It's finding the right information quickly.
That's where hash tables become incredibly useful.
🗃️ Hash Table — Think of Labeled Drawers
Imagine a large cabinet.
Instead of opening every drawer looking for someone's file, each drawer has a label.
┌──────────────────┐ │ user_101 → Amina │ ├──────────────────┤ │ user_205 → Karim │ ├──────────────────┤ │ user_312 → Sadia │ └──────────────────┘
You already know the label.
So you can ask:
Give me user_205.A Hash Table, also called a Hash Map, stores information using key-value pairs.
Key Value ───────── ───────────── user_101 → Amina user_205 → Karim user_312 → Sadia
In JavaScript, Map gives us this kind of behavior.
const users = new Map();
users.set("user_101", "Amina");
users.set("user_205", "Karim");
users.set("user_312", "Sadia");
console.log(users.get("user_205"));
Output:
Karim
Instead of asking:
Where is Karim in the list?
we ask:
What value belongs to user_205?That small change in thinking is powerful.
🔑 Key → Value
A hash table connects a key to a value.
For example:
const products = new Map();
products.set("P100", {
name: "Keyboard",
price: 2500
});
products.set("P200", {
name: "Mouse",
price: 1200
});
Now:
console.log(products.get("P200"));
returns the information associated with P200.
Keys are useful when something already has a natural identifier:
- user ID
- product SKU
- email address
- username
- order number
💡 If you frequently search for something using a unique key, a hash table should come to mind.
🧩 But How Does Hashing Work?
Imagine a warehouse with numbered storage locations.
You provide a key:
ORDER-5842
Behind the scenes, a process turns that key into a location where the value can be stored.
Conceptually:
Key ↓ ORDER-5842 ↓ Hashing ↓ Index / Location ↓ Order Data
That process is called hashing.
A hash function takes a key and helps determine where its value should live.
You do not need to understand advanced hash-function mathematics as a beginner.
The useful intuition is:
Key → Hashing → Location → Value
Because the structure knows where to look, hash tables can often perform lookups very quickly.
💥 What Happens When Two Keys Choose the Same Place?
Imagine two customers receive the same locker number.
customer_A ─┐
├──→ Locker 12
customer_B ─┘
Now we have a problem.
This is called a collision.
Two different keys produced the same storage location.
Real hash tables have ways to handle this.
One basic idea is to let the location store multiple entries and then distinguish between their keys.
Conceptually:
Locker 12 │ ├── customer_A → Amina │ └── customer_B → Karim
There are several collision-handling techniques, but we don't need to dive into their implementations yet.
For now:
💡 Collision = different keys trying to use the same hash location.
The important thing is that collisions are normal and hash tables are designed to deal with them.
🎟️ Set — When You Only Care About Unique Values
Suppose your website tracks which categories a customer has visited:
Books Technology Books Fashion Technology Books
But you only want the unique categories.
You don't need:
Books Books Books
You only need:
Books Technology Fashion
This is where a Set is useful.
const categories = new Set();
categories.add("Books");
categories.add("Technology");
categories.add("Books");
categories.add("Fashion");
console.log(categories);
Even though "Books" was added twice, it appears only once.
🔍 Membership Checking
Sets are also useful when the real question is:
Does this value already exist?
const blockedUsers = new Set([
"user_12",
"user_25",
"user_87"
]);
console.log(blockedUsers.has("user_25"));
Output:
true
This kind of membership checking appears constantly in programming.
For example:
- Has this user already voted?
- Have we visited this page?
- Is this permission enabled?
- Have we already processed this ID?
- Does this tag already exist?
Hash Map → Key points to a value
Set → We mainly care whether a unique value exists
We'll meet sets again when we explore graphs, DFS, and BFS.
🚨 What If Some Items Are More Important Than Others?
Now imagine you're building a hospital waiting system.
Three patients arrive:
Patient A → Normal Patient B → Emergency Patient C → Normal
A normal queue would process them by arrival time:
A → B → C
But that may not make sense here.
The emergency patient should probably be handled first.
So instead of asking:
Who arrived first?
we ask:
Who has the highest priority?
That's the idea behind a Priority Queue.
⭐ Priority Queue — Importance Before Arrival
Each item has a priority.
Task Priority ────────────────────── ──────── Update profile 3 Process payment 2 Critical system alert 1
If smaller numbers represent higher priority, the system should handle:
Critical system alert
↓
Process payment
↓
Update profile
The order is based on priority, not simply insertion time.
Priority queues are useful for things like:
- 🚨 emergency processing
- 📋 task scheduling
- 🌐 network operations
- 🗺️ pathfinding
- ⚙️ operating system jobs
But how do we repeatedly find the highest-priority item efficiently?
This leads us to heaps.
🏔️ Heap — Keep the Important Item Near the Top
A heap is a tree-like data structure designed so that an important value stays near the top.
There are two common types.
🔽 Min Heap
In a Min Heap, the smallest value stays at the top.
2
/ \
5 8
/ \
9 12
Notice the parent-child relationship.
Each parent is smaller than or equal to its children.
So the smallest value is easy to find:
2
Think:
Min Heap → smallest item has priority
🔼 Max Heap
A Max Heap does the opposite.
20
/ \
15 18
/ \
7 10
The largest value stays at the top.
Think:
Max Heap → largest item has priority
👨👧 Parent and Child
Heaps are structured using parent-child relationships.
Parent
/ \
Child Child
But unlike the trees we'll study in Part 3, heaps are mainly concerned with maintaining their priority rule.
For a Min Heap:
Parent ≤ Children
For a Max Heap:
Parent ≥ Children
This makes it efficient to repeatedly retrieve the most important item.
📦 Priority Queue vs Heap
These two ideas are closely related, but they are not exactly the same thing.
A Priority Queue describes the behavior we want:
Always give me the highest-priority item.
A Heap is a common data structure used to make that behavior efficient.
Think of it like this:
⭐ Priority Queue → What we want
🏔️ Heap → One common way to make it happen
You don't need to build a complete heap implementation yet.
For now, understanding why it exists is more useful.
🧠 Build the Mental Pictures
Just like Part 1, don't memorize long definitions.
Remember the problem each structure solves.
🗃️ Hash Table
Labeled drawers
Use a key to find its associated value quickly.
🎟️ Set
Unique guest list
Store unique values and quickly ask whether something exists.
⭐ Priority Queue
Emergency waiting line
Important items can move ahead of less important ones.
🏔️ Heap
Important item stays near the top
Useful for efficiently managing priorities.
🎯 Start Asking Better Questions
When you see a programming problem, ask what kind of lookup you actually need.
Do I need:
"Give me the value for this key"
Think Hash Table.
Do I need:
"Has this value already appeared?"
Think Set.
Do I need:
"Give me the most important item next"
Think Priority Queue.
Do I repeatedly need the smallest or largest priority?
Think Heap.
💡 DSA becomes easier when you stop memorizing names and start recognizing behaviors.
🌳 Where Do We Go Next?
So far, most of our structures have felt fairly flat.
Arrays form sequences.
Linked lists form chains.
Hash tables connect keys to values.
But real information isn't always flat.
Think about:
Company ├── Engineering │ ├── Frontend │ └── Backend │ └── Marketing
Or:
Computer ├── Documents ├── Pictures └── Projects
Now our information has levels.
Parents.
Children.
Branches.
That means we need a different kind of structure.
Up Next → Part 3
Trees: When Data Gets Hierarchical 🌳
Because fast lookup is useful — but sometimes the real challenge is representing how information belongs together.
Skilled full stack developer with expertise in modern web technologies and frameworks.