Top K Frequent Numbers

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <queue> #include <map> #include <algorithm> using namespace std; typedef pair<int , int > ppi; int main() { int k; cin>>k; int n; cin>>n; int A[n]; for(int i=0; i<n ; i++){ cin>>A[i]; } map<int ,int> mp; for(int i=0 ; i<n ; i++){ mp[A[i]]++; } priority_queue<ppi, vector<ppi>, greater<ppi>> minh; for(auto i = mp.begin(); i!=mp.end() ; i++){ minh.push({i->second , i->first}); if(minh.size()>k) minh.pop(); } while(k>0){ cout<<minh.top().second<<" "; minh.pop(); k--; } }
Editor Settings
Theme
Key bindings
Full width
Lines