Skip to content

Commit 5575f5f

Browse files
authored
top k frequent elements solution
1 parent aa011de commit 5575f5f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Solution {
2+
def topKFrequent(nums: Array[Int], k: Int): Array[Int] = {
3+
return nums.groupBy(identity)
4+
.view.mapValues(_.size)
5+
.toSeq
6+
.sortBy(- _._2)
7+
.take(k)
8+
.map(_._1)
9+
.toArray
10+
}
11+
}

0 commit comments

Comments
 (0)