Advertisement
selebry

dsadsa

Oct 26th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. fun kMostFrequentWords(words: List<String>, k: Int): List<String> {
  2. val wordCount = mutableMapOf<String, Int>()
  3.  
  4. for (word in words) {
  5. wordCount[word] = wordCount.getOrDefault(word, 0) + 1
  6. }
  7. // println(wordCount)
  8. val sortedWords = wordCount.entries.sortedWith(compareBy({ -it.value }, { it.key }))
  9.  
  10. val result = sortedWords.take(k).map { it.key }
  11.  
  12. return result
  13. }
  14.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement