Created
February 20, 2023 03:08
-
-
Save boysbee/e9837dfa4bd0d9a40b9bdf37c162ed8c to your computer and use it in GitHub Desktop.
Count Word and Find Rank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Map<Int, List<Char>>.findRank(rank: Int): List<Char> { | |
val e = this.entries.filterIndexed { index, entry -> | |
index + 1 == rank | |
} | |
return e.map { it.value }.flatten() | |
} | |
fun countWord(input: String): Map<Int, List<Char>> { | |
return input.groupBy { it } | |
.mapValues { (_, values) -> values.size } | |
.entries | |
.sortedByDescending { it.value } | |
.groupBy { it.value } | |
.mapValues { (k, v) -> | |
v.map { | |
it.key | |
} | |
} | |
} | |
countWord("aabbcccddeeeeffffd") | |
.findRank(2).forEach { | |
println(it) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment