Frequency

Frequency using array and map
 avatar
PEGG
c_cpp
10 days ago
769 B
1
Indexable
Never
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>

using  namespace std;

int main() {

    //using array

    int arr[]={3,3,2,1,5,3,2,1,4};
    int size=sizeof(arr)/sizeof(arr[0]);
    vector <int> freq(10);

        for (int j = 0; j < size ; ++j)
        {
               freq[arr[j]]++;
        }

    for (int i = 0; i < 10 ; ++i)
    {
        if(freq[i]>0) {
            cout << i << " " << freq[i];
            cout << endl;
        }
    }

    // using map

    map<int,int> m;

    int s;
    cin>>s;
    for (int i = 0; i < s ; ++i) {
        int in;
        cin>>in;
        m[in]++;
    }

    for (auto mm : m) {
        cout<<mm.first<<" "<<mm.second <<endl;
    }

    return 0;
}
Leave a Comment