Header Ads Widget

Counting frequency of the character by using map function / implementation of map /stl c++

 Here we are going to implement a map function of STL . and by using this map function we will count the frequency of the elements. 

Code:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int ,int > m;
int t;
cout<<"Enter number of elements"<<endl;
cin>>t;
cout<<"Enter the elements"<<endl;
while(t--)
{
int a;
cin>>a;
m[a]+=1;
}
cout<<"The Frequency of the elements : "<<endl;
for(auto &p:m)
cout<<p.first<<" "<<p.second<<endl;
return 0;
}

Output:-

Enter number of elements
5
Enter the elements
1
2
3
1
2
The Frequency of the elements :
1 2
2 2
3 1



Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-