I have a string "Hello World"
I need a Map<Char, Int>
which will contain the pairs of each character and the number of times it appears in the string : {H=1, e=1, l=3, o=2,r=1, d=1}
How can I do that without using the traditional for loop?
Do it like this
val str = "Hello World"
val m: Map<Char, Int> = str.groupingBy { it }.eachCount()
The maps toString
representation will look like this (note the one empty key which is the space character):
{H=1, e=1, l=3, o=2, =1, W=1, r=1, d=1}