Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Iterate once, doing `count[x] = count.getOrDefault(x, 0) + 1`. The map is the frequency table. O(n) time, O(k) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reach for a frequency map and handle the first-sighting default.
Where candidates slip
NPE on missing key, or an O(n²) count-per-value loop.
How to stand out
Name the downstream problems (anagram, top-K, majority) this map unlocks.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Hash map counting
1Hint 1Explore hintHide hint
What structure maps a value to a running count?
2Hint 2Explore hintHide hint
Increment count[x], defaulting to 0 for first sightings.
3Hint 3Explore hintHide hint
This same map powers anagrams, top-K, and majority element.
A · Core idea
Build a frequency map: a hash map from each value to its count, filled in a single pass with count[x] = count.getOrDefault(x, 0) + 1. This table is the single most reused primitive from fresher problems all the way to hard ones.
Why getOrDefault / Counter. The first time you see a value it has no entry; getOrDefault(x, 0) (Java) or collections.Counter (Python) supplies the implicit 0 so you don't NullPointer or KeyError on first sight.
Why O(n) time, O(k) space. One pass over n elements, each an O(1) map update; the map holds k distinct keys, so space is O(k) ≤ O(n).
Why it matters. Anagram checks, top-K frequent, majority element, group-by, and first-unique-character all reduce to building this map first. Learn the increment idiom cold and a whole family of problems opens up.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 2, 3]1
new → 1
{1:1}{1:1, 2:2, 3:1}C · Code with teaching comments
D · Why it works and complexityO(n) timeO(k) spaceOpen the invariant and proofExplore the proofHide the proof
This single building block unlocks a huge family of harder problems — learn it cold.
ComplexityO(n) time, O(k) space — one O(1) map update per element (O(n) total); the map stores k ≤ n distinct keys.
E · Edge cases and pitfalls4nums = [5, 5, 5] — Single key → {5:3}.Explore edge casesHide edge cases
Edge cases handled
nums = [5, 5, 5]Single key → {5:3}.nums = []Empty map {}.
Pitfalls
- Forgetting the default for a first appearance (NullPointerException in Java).
- Scanning a list per value (O(n²)) instead of one map pass.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic java.util.Map<Integer, Integer> frequency(int[] nums) { java.util.Map<Integer, Integer> freq = new java.util.HashMap<>(); for (int x : nums) { freq.put(x, freq.getOrDefault(x, 0) + 1); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1No default on first sight
`freq.get(x) + 1` throws when x is absent (null in Java). Use getOrDefault / Counter so first sightings start at 0.
Explore the code comparisonHide the code comparison
No default on first sight
`freq.get(x) + 1` throws when x is absent (null in Java). Use getOrDefault / Counter so first sightings start at 0.
Don't write this
freq.put(x, freq.get(x) + 1); // NPE when x is newWrite this instead
freq.put(x, freq.getOrDefault(x, 0) + 1);Revise
Carry the pattern into the interview
Pattern to remember
Frequency map
Code pattern to memorize
for x: freq[x] = freq.get(x,0) + 1Rules to remember
- 1Key = value, value = count.
- 2getOrDefault for first sighting.
- 3Foundation for anagram/top-K.
Use this pattern when
- Counting, anagrams, top-K, majority, group-by
Interview takeaway
The frequency map is the most reused fresher-to-advanced primitive.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Frequency map. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Valid AnagramCompare two frequency maps (or one with +/−).
- Top K Frequent ElementsComing soonBuild this map, then bucket/heap by count.