Skip to content

Commit c1838af

Browse files
committed
top-k-frequent-elements solution
1 parent 6de4775 commit c1838af

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

top-k-frequent-elements/Yu-Won.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 문제: https://leetcode.com/problems/top-k-frequent-elements/description/
3+
*
4+
* 요구사항:
5+
* nums: number[], k: number 를 Input 으로 받았을 때
6+
* 가장 빈도가 높은 값 k개의 값을 number[]로 리턴한다.
7+
*
8+
* * */
9+
10+
const topKFrequent = (nums, k) => {
11+
const map = new Map();
12+
13+
for(let i = 0; i < nums.length; i++) {
14+
const count = map.get(nums[i]) || 0;
15+
map.set(nums[i], count+1);
16+
}
17+
18+
const entries = [...map.entries()];
19+
entries.sort((a,b) => b[1]-a[1]);
20+
21+
return entries.slice(0,k).map((entry) => entry[0]);
22+
}

0 commit comments

Comments
 (0)