fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. bool cmp(pair<int,int> p1,pair<int,int> p2){
  4. if(p1.second==p2.second)
  5. return p1.first<p2.first;
  6. return p1.second>p2.second;
  7. }
  8.  
  9. int main() {
  10.  
  11. int a[]={5,5,4,6,4};
  12. unordered_map<int,int> freq;
  13. for(auto x: a){
  14. freq[x]++;
  15. }
  16. vector<pair<int,int>> v;
  17. for(auto x:freq){
  18. v.push_back(make_pair(x.first,x.second));
  19. }
  20. sort(v.begin(),v.end(),cmp);
  21.  
  22. for(auto x: v){
  23. while(x.second--){
  24. cout<<x.first<<" ";
  25. }
  26. }
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 4328KB
stdin
Standard input is empty
stdout
4 4 5 5 6