Skip to content
Justice Tech School

Practice: Word Frequency

Split, group and sort.

Example
val text = "the cat and the hat and the bat"
val freq = text.split(" ").groupingBy { it }.eachCount()
freq.entries.sortedWith(compareByDescending<Map.Entry<String, Int>> { it.value }.thenBy { it.key }).forEach { println("${it.key}: ${it.value}") }

Exercise

Count groups.

groupingBy { it }.()

Quiz Test

What does eachCount return?

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