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 e3b111d commit e51af94Copy full SHA for e51af94
top-k-frequent-elements/RiaOh.js
@@ -0,0 +1,22 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @param {number} k
4
+ * @return {number[]}
5
+ */
6
+var topKFrequent = function (nums, k) {
7
+ const obj = {};
8
+ for (let i = 0; i < nums.length; i++) {
9
+ if (Object.keys(obj).includes(String(nums[i]))) {
10
+ obj[nums[i]] = obj[nums[i]] + 1;
11
+ } else {
12
+ obj[nums[i]] = 1;
13
+ }
14
15
+
16
+ const keysArr = Object.keys(obj);
17
+ const sortedObj = keysArr
18
+ .sort((a, b) => obj[b] - obj[a])
19
+ .slice(0, k)
20
+ .map((num) => Number(num));
21
+ return sortedObj;
22
+};
0 commit comments