Skip to content

Commit ce6a63e

Browse files
committed
[week1] Top K Frequent Elements - gmlwls96
1 parent 1774ef3 commit ce6a63e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)