-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathsort_elements_by_frequency.cpp
81 lines (64 loc) · 1.7 KB
/
sort_elements_by_frequency.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Given an array of integers, sort the array according to frequency of elements.
// That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first.
// Input:
// N = 5
// A[] = {5,5,4,6,4}
// Output: 4 4 5 5 6
class Solution{
public:
//custom comparator function to sort map according to frequencies
static bool cmp( pair<int, int> &a, pair<int, int> &b)
{
if(a.second==b.second)
return a.first<b.first;
return a.second>b.second;
}
vector<int> sortByFreq(int arr[],int n)
{
vector<int>ans;
map<int,int> m;
vector<pair<int, int> > A;
//storing the elements as keys and counting its frequency
for(int i=0; i<n; i++)
{
int x= arr[i];
m[x]++;
}
// Copy key-value pair from Map
// to vector of pairs
for (auto &it : m) {
A.push_back(it);
}
//sorting the vector of pairs using cmp function
sort(A.begin(), A.end(), cmp) ;
for(auto x: A)
{
//printing the element the no. of times it occured in the actual array
while(x.second)
{
ans.push_back(x.first);
x.second--;
}
}
return ans;
}
};
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int a[n+1];
for(int i = 0;i<n;i++){
cin >> a[i];
}
Solution obj;
vector<int> v;
v = obj.sortByFreq(a,n);
for(int i:v)
cout<<i<<" ";
cout << endl;
}
return 0;
}