Async and Practical
Histogram Counts as Bars
Tallying a small sample into a Map<String, int> and rendering each count as a string of # characters builds a tiny histogram. The bar width is just '#' * count, which makes the proportions visible at a glance.
Program
Play the program to count six samples and render them as compact bars.
histogram.dart
void main() {
var samples = ['red', 'blue', 'red', 'green', 'red', 'blue'];
var counts = <String, int>{};
for (var s in samples) {
counts[s] = (counts[s] ?? 0) + 1;
}
var bars = counts.entries
.map((e) => '${e.key}: ${'#' * e.value}')
.toList();
print(bars.join(' | '));
}
counter map
`counts[s] = (counts[s] ?? 0) + 1` defaults missing keys to zero before adding one.
string repetition
`'#' * count` repeats `#` once per occurrence, turning a tally into a tiny bar.
insertion order
Default Dart maps keep insertion order, so the bars render in first-seen order.