The Beginning: Campus Placement Panic
So, my campus placements have officially started. And guess what? The moment I saw the word DSA on the prep list, I could literally hear my brain whispering, "Run!" π
But being the brave soul (and caffeine-powered coder) that I am, I decided to face it head-on. I opened a random problem, stared at it for 10 minutes like a detective at a crime scene, and then thought...
lol?
My Face
but one thing i know,
hashmaps can help. They always do something magical.
And yes, I was right.
So... What Exactly Is a Hashmap?
If youβre also like me β someone who used hashmaps before but didnβt really understand them β letβs clear that up.
A hashmap is like your brainβs quick memory. You remember your friendβs name (the key) and instantly recall their face (the value).
Thatβs exactly what hashmaps do β they store stuff in key-value pairs.
In C++, itβs called an unordered_map.
you can also use ordered_map as well.
Example Declaration:
unordered_map<int, int> m; // key: int, value: int
Some Common Operations:
m[key] = value; // Store a value
m[key]++; // Increment the frequency of key
m.find(key); // Check if key exists
m.erase(key); // Remove key-value pair
And the best part β all these operations are super fast, like O(1) average time. Pretty neat, right?
The Problem: "Lost Attendance Sheet" π
So hereβs the story.
Imagine youβre a teacher, and you have a list of studentsβ roll numbers who attended class. Everything looks fine until... someone signs the attendance sheet twice! π
You now need to find which roll number is the duplicate.
Input:
[1, 3, 4, 2, 2]
Output:
2
Thinking Like a Problem Solver π§
Step 1: Understand Input and Output
- Input β an array of integers
- Output β the roll number that appears twice
Step 2: Think About the Logic
We need to count how many times each number appears.
If any number appears twice β thatβs our culprit!
Step 3: Choose the Right Data Structure
We need quick insert + quick search = Hashmap β
Key β roll number
Value β frequency count
My First Attempt (aka the "Oops" moment)
unordered_map<int,int> m;
for(int i : arr) {
m[arr[i]]++;
}
for(auto i : m) {
if(m.second() >= 2) return m.first();
}
π‘ Mistake alert! β I wrote m.second() and m.first() with brackets like theyβre functions.
But nope β theyβre members, not functions.
It should be:
if(i.second >= 2) return i.first;
Classic rookie move. Happens to the best of us. π
Final Working Code
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int findDuplicate(vector<int>& arr) {
unordered_map<int, int> m;
for (int i : arr) {
m[i]++;
}
for (auto i : m) {
if (i.second >= 2)
return i.first;
}
return -1; // no duplicate found
}
int main() {
vector<int> arr = {1, 3, 4, 2, 2};
cout << "Duplicate roll number: " << findDuplicate(arr);
return 0;
}
Dry Run (Because Visuals Help)
arr = [1, 3, 4, 2, 2]
After the first loop β hashmap looks like this:
{1:1, 3:1, 4:1, 2:2}
Second loop β finds that 2 has frequency 2.
β
. Duplicate found.
Output:
Duplicate roll number: 2
All done in O(n) time, O(n) space.
Hashmaps doing their magic again πͺ
Why This Works
Normally, youβd have to compare every element with every other element β O(nΒ²) time.
But hashmaps let you store and check everything in one pass β O(n) time.
Basically, theyβre like the cheat codes of DSA problems.
Final Takeaway: How to Think in DSA
Over time, Iβve realized that solving DSA problems boils down to three things:
- Understand the input and output β What are we working with?
- Find the logic and right data structure β What helps us solve it fastest?
- Apply and tweak β Turn logic into working code, and fix when it doesnβt work (like my first attempt π )
The End (or Maybe Just the Beginning)
Hashmaps are that one friend in DSA who always has your back. They might look scary at first, but once you get them β oh boy, they make your life so much easier. π
So next time you feel lost in a jungle of arrays and strings, remember:
Hashmaps might just be your map πΊοΈ.

Top comments (0)