Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Count how many times each letter appears in banana. Use a dictionary.
counts[x] = counts.get(x, 0) + 1
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, compare your working with the solution.
Plan. An empty dictionary. For each letter, add 1 to its count. The first time, get gives 0.
The result is {'b': 1, 'a': 3, 'n': 2}.
Check. , the number of letters in banana. ✓
Log in to join the discussion.
Loading…