Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> topKFrequent(vector<int>& nums, int k) {
- unordered_map<int,int>mp; //To make the frequency table
- int n = nums.size();
- for(int i =0; i<n; i++){
- mp[nums[i]]++;
- }
- priority_queue<pair<int,int>> pq;
- for(auto it = mp.begin(); it!=mp.end(); it++){
- pq.push({it->second, it->first});
- }
- vector<int> ans;
- while(k!=0){
- ans.push_back(pq.top().second);
- pq.pop();
- k--;
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement