Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
sortbyfrequency(hasing+sorting using comparator function)
#include<bits/stdc++.h> using namespace std; bool compare(pair<int,int>p1,pair<int,int>p2) { if(p1.second!=p2.second) { return (p1.second>p2.second); } else { return (p1.first>p2.second); } } void sortByFreq(int arr[],int n) { //Your code here unordered_map<int,int>m; for(int i=0;i<n;i++) { m[arr[i]]++; } vector<pair<int,int>>v; for(auto it=m.begin();it!=m.end();it++) { v.push_back(make_pair(it->first,it->second)); } sort(v.begin(),v.end(),compare); for(int i=0;i<v.size();i++) { int k=v[i].second; while(k--) { cout<<v[i].first<<" "; } } cout<<"\n"; } int main() { int t; cin>>t; while(t--) { int n; cin>>n; int arr[n]; for(int i=0;i<n;i++) { cin>>arr[i]; } sortByFreq(arr,n); } }
run
|
edit
|
history
|
help
0
Trace
HeatPump COP
semiprog.cpp
Substring search
11aa11
Stream11
Scope guarding
Polyrmophism
შუალედური მეოთხე საკითხი.Mobile tariff "Cents"
alternate list