How many of each
A survey tallies how often each answer was given.
- Count how many times each distinct string appears.
- Every distinct value that appears earns an entry in the result.
group_sizes(values: list<string>) → map<string, int>
Where you start
def group_sizes(values: list[str]) -> dict[str, int]:
Worked examples
| Call | Result |
|---|---|
group_sizes(["a", "b", "a"]) | {"a": 2, "b": 1} |
group_sizes(["x"]) | {"x": 1} |
group_sizes(["p", "p", "p"]) | {"p": 3} |
group_sizes(["a", "b", "c"]) | {"a": 1, "b": 1, "c": 1} |
Hint
Add one to the map entry for each value you meet.
Reference solution in Python
def group_sizes(values: list[str]) -> dict[str, int]:
result = {}
for v in values:
result[v] = result.get(v, 0) + 1
return result