We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1774ef3 commit ce6a63eCopy full SHA for ce6a63e
top-k-frequent-elements/gmlwls96.kt
@@ -0,0 +1,15 @@
1
+class Solution {
2
+ fun topKFrequent(nums: IntArray, k: Int): IntArray {
3
+ val answer = IntArray(k)
4
+ val set = nums.toSet()
5
+ val mutableList = mutableListOf<Pair<Int,Int>>()
6
+ set.forEach { num ->
7
+ mutableList.add(num to nums.count { it == num})
8
+ }
9
+ mutableList.sortByDescending { it.second }
10
+ for (i in 0 until k){
11
+ answer[i] = mutableList[i].first
12
13
+ return answer
14
15
+}
0 commit comments