Skip to content

Commit 03ee829

Browse files
committed
top k frequent elements
1 parent d8a6069 commit 03ee829

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
fun topKFrequent(nums: IntArray, k: Int): IntArray {
3+
val countMap = nums.groupBy { it }
4+
.mapValues { it.value.size }
5+
val sortedList = countMap.entries
6+
.sortedByDescending { it.value }
7+
.map { it.key }
8+
return sortedList.subList(0, k).toIntArray()
9+
}
10+
}

0 commit comments

Comments
 (0)