What Is a Frequency Count? Tallying Values in One Pass
A frequency count stores how many times each value has appeared. One pass updates the tally, and a tie keeps the value that showed up first.
A frequency count is a hash map from a value to how many times you have seen it. The key is the value. The stored number is the count. You meet it after Two Sum, which stored an index instead of a tally.
How it works
Start with an empty map. For each value, read the current count, add one, and write it back. A missing key starts at zero. After one pass the map holds every tally, and the time is O(n) if each update is O(1) on average. The extra space is O(n) when every value is unique.
The most frequent value is the key with the largest final count. A second walk over the original array considers candidates in first-appearance order. Replace the winner only when a count is strictly larger. Equal counts therefore leave the earlier candidate in place without storing first indexes separately.
A counter for one target does not need a map. The map is the tool when you do not know which value will win, or when several targets share the same pass.
Press Run to execute this example. Use console.log to print a value.
Expected output: 2, then null. The second pass visits 4 before 2, but 2 has the larger final count. If their final counts were equal, the strict > update would preserve the value encountered first.
Worked example
For [4, 2, 4, 2, 2], the first pass builds { "2": 3, "4": 2 }. The second pass first makes 4 the winner with count 2, then replaces it with 2 because 3 is larger. Later copies do not change the result.
Why it is correct
After the first loop has processed a prefix, counts[String(value)] equals the number of copies of that value in the prefix. Updating exactly the next value preserves that invariant, so the completed table has every final frequency. The second loop keeps the highest-frequency value seen so far. Because it updates only for a strictly larger count, equal-frequency values cannot displace the one encountered first.
Cost
The two sequential passes take O(n) time, not O(2n): constants disappear in asymptotic notation. With u distinct values, the count object uses O(u) extra space, at most O(n). Empty input returns null.
In the course
Hash maps introduce the complement. The next lesson, Counting with a map, asks for the tally of one target and then for the most frequent value, with the earliest value winning a tie. See also How a Hash Map Remembers What You Have Already Seen.
Common mistakes
Replacing the winner when the counts are equal picks the later value. The tie rule wants the earlier one.
Number keys and string keys are different stores. Pick one and stay with it. String(value) is a safe object key.
A nested loop that recounts from scratch returns the right number and hides the map. The point of the pass is that each value is touched once.
When to use it
Use a frequency count when the question is "how many" or "which value appears most", and the values are the keys. It is the same space-for-time trade as Two Sum.
When not to
Do not build the map when you only need a sum or a single comparison. A counter is enough, and the map adds a structure you then have to read back.
Problem-solving checklist
State what each key and value mean. Decide the empty-input result and tie rule before coding. Test one unique winner, one tie, one repeated value, and the empty array. Finally, check that no loop restarts a count from scratch.
Optional enrichment
Return both the winning value and its count without a second counting pass. Preserve the earliest value on a tie, and explain which state must be updated together.