Skip to content

Instantly share code, notes, and snippets.

@boysbee
Created February 20, 2023 03:08
Show Gist options
  • Save boysbee/e9837dfa4bd0d9a40b9bdf37c162ed8c to your computer and use it in GitHub Desktop.
Save boysbee/e9837dfa4bd0d9a40b9bdf37c162ed8c to your computer and use it in GitHub Desktop.
Count Word and Find Rank
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