All lessons

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

Many problems boil down to "how often does each thing show up?" A Dictionary is perfect: keys are the items, values are counts.

Increment with counts[x] = counts.GetValueOrDefault(x, 0) + 1 as you scan the input once.

You
Bug Slime

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: 3