Skip to content

Add Top K Frequent Elements #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Top K Frequent Elements
https://leetcode.com/problems/top-k-frequent-elements/description/

Given an integer array nums and an integer k, return the k most frequent elements.
You may return the answer in any order.


Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]


Example 2:

Input: nums = [1], k = 1
Output: [1]


Constraints:

1) 1 <= nums.length <= 10^5
2) -10^4 <= nums[i] <= 10^4
3) k is in the range [1, the number of unique elements in the array].
4) It is guaranteed that the answer is unique.
*/

/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
const freqMap = new Map();
const bucket = [];
const result = [];

for(let num of nums) {
freqMap.set(num, (freqMap.get(num) || 0) + 1);
}

for(let [num, freq] of freqMap) {
bucket[freq] = (bucket[freq] || new Set()).add(num);
}

for(let i = bucket.length-1; i >= 0; i--) {
if(bucket[i]) {
result.push(...bucket[i]);
if(result.length === k) break;
}
}
return result;
};

module.exports.topKFrequent = topKFrequent;
10 changes: 10 additions & 0 deletions LeetcodeProblemsTests/Algorithms/Top_K_Frequent_Elements_Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const assert = require('assert');
const topKFrequent = require('../../LeetcodeProblems/Algorithms/Top_K_Frequent_Elements').topKFrequent;

var test = function () {
assert.deepEqual(topKFrequent([1,1,1,2,2,3], 2).sort(), [1,2]);
assert.deepEqual(topKFrequent([7,8,9,8,9,8], 2).sort(), [8,9]);
assert.deepEqual(topKFrequent([1,1,1,1], 1).sort(), [1]);
}

module.exports.test = test;
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
| [Minimize Maximum Pair Sum in Array ](/LeetcodeProblems/Algorithms/Minimize_Maximum_Pair_Sum_In_Array.js) | Medium | https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/ |
| [Top K Frequent Elements ](/LeetcodeProblems/Algorithms/Top_K_Frequent_Elements.js) | Medium | https://leetcode.com/problems/top-k-frequent-elements/ |
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |
Expand Down