val fruits = List("apple", "banana", "avocado", "papaya") val countsToFruits = // count how many 'a' in each fruit fruits.groupBy(fruit => fruit.count(_ == 'a')) for (count, fruits) <- countsToFruits do println(s"with 'a' Ã $count = $fruits") // prints: with 'a' Ã 1 = List(apple) // prints: with 'a' Ã 2 = List(avocado) // prints: with 'a' Ã 3 = List(banana, papaya)
{{#tags}}- {{label}}
{{/tags}}