So I have an array as :
arr[] = {5, 2,4,2,3,5,1};
How can I insert them in this order with the number of times they occur in unordered_map?
#include<bits/stdc++.h>
using namespace std;
void three_freq(int arr[], int n){
unordered_map<int, int> m;
for(int i=0;i<n;i++){
m[arr[i]]++;
}
for(auto itr = m.begin(); itr != m.end(); itr++){
cout<<itr->first<<":"<<itr->second<<"\n";
}
}
int main(){
int arr[] = {5, 2,4,2,3,5,1};
int n = sizeof(arr)/ sizeof(arr[0]);
three_freq(arr, n);
return 0;
}
Using the code above I am getting output as :
1:1
3:1
4:1
5:2
2:2
But I want the output to be in same order as the element occur in array. Example:
5:2
2:2
4:1
3:1
1:1