Skip to content

Commit d3a4c76

Browse files
committed
Add top K frequent elements solution
1 parent 6559206 commit d3a4c76

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
fun topKFrequent(nums: IntArray, k: Int): IntArray {
3+
val frequency = nums.toList().groupingBy { it }.eachCount()
4+
5+
return frequency
6+
.toList()
7+
.sortedByDescending { it.second }
8+
.take(k)
9+
.map { it.first }
10+
.toIntArray()
11+
}
12+
}

0 commit comments

Comments
 (0)