Skip to content

Commit

Permalink
Time: 19 ms (53.33%), Space: 13.7 MB (68.96%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed May 22, 2023
1 parent c7019c1 commit 10bb256
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 0347-top-k-frequent-elements/0347-top-k-frequent-elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {

int n=nums.size();
unordered_map<int,int>mp;
vector<int> Res;


for(int i=0;i<n;i++)
{
mp[nums[i]]++;
}

priority_queue<pair<int,int>> m;
for( auto i : mp)
{
m.push(make_pair( i.second, i.first));
}

for( int i =0;i<k ; i++)
{
Res.push_back( m.top().second);
m.pop();
}

return Res;
}
};

0 comments on commit 10bb256

Please sign in to comment.