Tsallaka zuwa abun ciki
Justice Tech School

Practice: Word Frequency

Split, count in a map and sort.

Example
void main() {
  var words = "the cat and the hat and the bat".split(" ");
  var freq = <String, int>{};
  for (var w in words) freq[w] = (freq[w] ?? 0) + 1;
  var e = freq.entries.toList()..sort((a, b) => b.value.compareTo(a.value));
  for (var x in e) print("${x.key}: ${x.value}");
}

Exercise

Default 0.

(freq[w]  0) + 1

Quiz Test

What does ?? 0 do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.