Problem 201296 · easy · Phase 02 Linear Data Structures

Count Into Bins

arrays · counting · histograms

Given a list values of integers, each between 0 and m - 1, and the number of bins m, return a list of length m whose entry at index v is how many times v occurs in values.

Examples

Input:  values = [0, 2, 2, 3, 0], m = 4
Output: [2, 0, 2, 1]
Explanation: 0 appears twice, 1 never, 2 twice, 3 once.

Input:  values = [], m = 3
Output: [0, 0, 0]

Constraints

  • 0 <= len(values) <= 10**5
  • 0 <= m <= 10**5 and 0 <= values[i] < m
  • Target: O(n + m) time; no sorting and no values.count(...) per bin.

Goals

  • Use list indices as bin labels
  • Build a fixed-size count list with `[0] * m`
  • Count in a single pass without sorting
Starting Python…