Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun kMostFrequentWords(words: List<String>, k: Int): List<String> {
- val wordCount = mutableMapOf<String, Int>()
- for (word in words) {
- wordCount[word] = wordCount.getOrDefault(word, 0) + 1
- }
- // println(wordCount)
- val sortedWords = wordCount.entries.sortedWith(compareBy({ -it.value }, { it.key }))
- val result = sortedWords.take(k).map { it.key }
- return result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement