Overview
Hash map counting: O(1) average lookup se elements count aur track karna.
Analogy
Jaise attendance sheet—har naam (key) kitni baar sign in hua (value) track karo.
Step-by-step
- Khaali dictionary banao
- Har element par iterate karo
- Exist karta hai toh +1, warna 1 se initialize karo
- .get(key,0) se KeyError se bachao
Visual
words = ["cat","bat","cat","hat"]
counts = {}
for w in words:
counts[w] = counts.get(w,0)+1
# {"cat":2,"bat":1,"hat":1}
Common mistakes
- Existence check ke bina dict[key] use karna
- Counter ke baare mein nahi jaanna
Practice questions
- 1 se n+1 ke integers array mein missing number dhundo
- Duplicates ko first aur re-occurrence mein classify karo