🏏 The 18-Day DSA Cover Drive | Day 2 — Meet Counter
Learn DSA one Python trick at a time.
What's up, DEV community! 👋
Welcome back to Day 2 of The 18-Day DSA Cover Drive.
🏆 Kohli Corner
Fastest to 8,000 ODI Runs
Virat Kohli didn't score 8,000 ODI runs in a single innings.
He reached the milestone by scoring one run at a time, trusting the process, and showing up consistently.
Just like great innings are built one run at a time, great code is built one improvement at a time.
📊 Day 2 — Meet Counter
🎯 Today's Python Power-Up
Yesterday, we learned how to build a Frequency Map manually.
freq = {}
for item in arr:
freq[item] = freq.get(item, 0) + 1
It works.
It's clean.
But Python says...
"Why write four lines when one line is enough?" 😄
Meet Counter.
✨ The Magic Line
from collections import Counter
arr = [1, 2, 1, 3, 2, 1]
freq = Counter(arr)
print(freq)
Output
Counter({1: 3, 2: 2, 3: 1})
That's it!
No loop.
No .get().
No if-else.
Just one line.
🤔 Imagine This...
Your teacher asks,
"How many students are wearing blue?"
❌ Yesterday's Way
You walk around the classroom and count everyone manually.
Again.
Again.
Again.
✅ Today's Way
The teacher presses a button on a smart attendance machine.
Blue → 8
Black → 12
White → 6
Instant result.
That's exactly what Counter does.
❌ Before
freq = {}
for item in arr:
freq[item] = freq.get(item, 0) + 1
✅ After
from collections import Counter
freq = Counter(arr)
✅ Cleaner
✅ Smaller
✅ More Pythonic
🚀 Bonus Powers of Counter
Count a specific element
print(freq[1])
Output
3
Find the most frequent element
print(freq.most_common(1))
Output
[(1, 3)]
Pretty cool, right? 😄
💡 Where is Counter useful?
You'll commonly use it in problems like:
- Finding duplicate elements
- Counting character frequencies
- Valid Anagram
- Majority Element
- Top K Frequent Elements
- Frequency-based array problems
⚡ Interview Insight
Should you always use Counter?
✅ Use Counter when the problem is mainly about counting frequencies.
❌ Don't immediately use Counter if an interviewer specifically asks you to implement the frequency map yourself.
Understanding how Counter works internally (which we learned on Day 1 using dict.get()) is just as important as knowing the shortcut.
First learn the logic. Then learn the shortcut.
🧠 Quick Challenge
Without running the code...
Can you guess the output?
from collections import Counter
word = "mississippi"
freq = Counter(word)
print(freq["s"])
print(freq.most_common(1))
👇 Drop your answer in the comments before checking!
🎯 Bonus Question
Can you think of another DSA problem where Counter can replace an entire counting loop?
I'd love to hear your answers!
🏏 Cover Drive of the Day
🏏 Concept: Counter
⭐ Python Gem: collections.Counter
💼 Interview Insight: Learn the logic first, then use the shortcut.
⏭️ Next Innings: defaultdict — A dictionary that creates missing keys automatically.
💬 Let's Discuss
Have you used Counter before?
Or did you learn something new today?
If you know another hidden Python gem that makes DSA easier, drop it in the comments! 👇
18DayDSACoverDrive | Python | DSA | Beginners | Learning
Top comments (0)