2

For example I have a list of strings like:

val list = listOf("a", "b", "a", "b" "a" "c") and I need to convert it to a Map, where the strings are the keys and values are count of repetitions. So i have to got Map like [a = 3] [b = 2] [c = 1]

1 Answer 1

2
val list = listOf("a", "b", "a", "b", "a", "c")

val result = list.groupingBy { it }.eachCount()

// result is Map<String, Int> looking like this: {a=3, b=2, c=1}

Edit: for counts bigger than 1 add a filter condition to the map

val result = list.groupingBy { it }.eachCount().filter { it.value > 1 }

// result is now: {a=3, b=2}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, and what if I need only pairs, where values > 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.