Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Как мне из
- //listOf("a" to 2, "a" to 3)
- //Сделать
- //listOf("a" to 5)
- fun main() {
- println(
- solve(
- listOf("a" to 2, "a" to 3) // -> [(a, 5)]
- )
- )
- println(
- solve(
- listOf("a" to 2, "a" to 3, "b" to 9, "c" to 0) // -> [(a, 5), (b, 9), (c, 0)]
- )
- )
- println(
- solve(
- listOf("a" to 2, "a" to 3, "b" to -1, "b" to 11, "c" to 0) // -> [(a, 5), (b, 10), (c, 0)]
- )
- )
- }
- fun solve(list: List<Pair<String, Int>>): List<Pair<String, Int>> {
- val result = mutableListOf<Pair<String, Int>>()
- val map = mutableMapOf<String, Int>()
- list.forEach {(k, v) ->
- map[k] = map.getOrDefault(k, 0) + v
- }
- val keys = map.keys
- keys.forEach {k ->
- result.add(k to map[k]!!)
- }
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement