Untitled
user_2773531
plain_text
a year ago
1.0 kB
1
Indexable
Never
#include <iostream> #include <bits/stdc++.h> using namespace std; vector<int> frequencySort(vector<int>& s) { int n = s.size(); vector<int> ans; vector<pair<int, int>> v; unordered_map<int, int> mp; for (int i = 0; i < n; i++) { mp[s[i]]++; } for (auto it : mp) { v.push_back({it.second, it.first}); } // sort(v.begin(), v.end(), greater<pair<int, int>>()); sort(v.begin(), v.end(),[](const pair<int,int>&a,const pair<int,int>&b){ return a.first > b.first ; }); for (int i = 0; i < v.size(); i++) { while (v[i].first != 0) { ans.push_back(v[i].second); v[i].first--; } } return ans; } int main() { int x; cin >> x; vector<int> v1(x); for (int i = 0; i < x; i++) { int n; cin >> n; v1[i] = n; } vector<int> sortedResult = frequencySort(v1); for (int i = 0; i < sortedResult.size(); i++) { cout << sortedResult[i] << " "; } return 0; }