Count each key
Warm-up · Write code · about 6 min
Given nums with every value between 0 and k, return an array count of length k + 1 where count[v] is how many times v appears in nums.
Example 1
Input:
[[2,0,3,0,2,1,2],3]
Output:
[2,1,3,1]
Example 2
Input:
[[],2]
Output:
[0,0,0]
No values, so every count is 0.
Constraints
- 0 ≤ nums.length ≤ 1,000
- 0 ≤ nums[i] ≤ k ≤ 100
Learn the idea first: Counting sort, a visual lesson.
More Counting sort problems
- Fill the count array Warm-up
- Implement counting sort Standard
- Fill in the stable placement Standard
- Spot the bug: equal keys swap order Standard
- Sort by a bounded key, stably Challenge