Increment the count for a key
Updated
Increment is put(k, getOrDefault(k,0)+1). First sighting creates the key at 1. Later sightings overwrite with a larger count. Size grows only on the first sighting.
What to focus on
Update then query, or query then update. Pick a consistent order when detecting first time count hits 2.
Worked examples
first sighting
a = [], inc 4- absent → 1
Result: {4:1}
second
a = [4, 1, 9, 1] running, inc the second 1- 1 was 1
- now 2
Result: {4:1, 1:2, 9:1}
new key among others
a = [4, 1] as {4:1,1:1}, inc 9- size 2→3
Result: {4:1, 1:1, 9:1}
Try it
Write inc(m, k). m is the frequency map. k is the key. Put k → getOrDefault(k, 0)+1. First sighting starts at 1. Size grows only then. a = [], inc 4 → {4:1}. a = [4, 1, 9, 1] running, inc the second 1 → {4:1, 1:2, 9:1}. a = [4, 1] as {4:1,1:1}, inc 9 → {4:1, 1:1, 9:1}.
keys are integers. First sighting of a key starts at 1.