Frequency Counting
6 min · C examples
Use a dictionary to count how many times each value appears.
C · Frequency Counting0/5 cleared
Enemy 1 of 5
This code goes through a list of words once and keeps a count of each word in a dictionary. Press Run to see how many times a shows up.
Why this works
Many problems boil down to "how often does each thing show up?" C has no built-in hash map, but a fixed-size array works great when items are letters: index by ch - 'a'.
Increment with counts[ch - 'a']++ as you scan the input once.
You
Cargo Cultist
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.