binCounts function

List<int> binCounts(
  1. List<int> binIndices,
  2. int bins
)

Frequency of each bin index in binIndices, over bins buckets.

Returns a list of length bins; indices outside 0..bins-1 are ignored so a stray out-of-range index cannot grow or corrupt the histogram. Audited: 2026-06-12 11:26 EDT

Implementation

List<int> binCounts(List<int> binIndices, int bins) {
  assert(bins >= 1, 'binCounts requires bins >= 1');
  final List<int> counts = List<int>.filled(bins, 0);
  for (final int index in binIndices) {
    // Skip out-of-range indices defensively rather than throwing on bad input.
    if (index >= 0 && index < bins) counts[index]++;
  }
  return counts;
}