Frequency Sort

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <queue> #include <map> using namespace std; int main() { 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<pair<int,int>> maxh; for(auto i= mp.begin(); i!=mp.end(); i++){ maxh.push({i->second , i->first}); } while(maxh.size()>0){ int f= maxh.top().first; int e = maxh.top().second; for(int i=0 ; i<f; i++) cout<<e<<" "; maxh.pop(); } }
Editor Settings
Theme
Key bindings
Full width
Lines