Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int m;
- cin>>m;
- if(m<=0||m>=100)
- {
- cout<<"Invalid input"<<endl;//checking if m satisfies the conditions
- return 0;
- }
- int num,i,mx=0,ans=0;
- unordered_map<int,int>mp;
- for(i=0;i<m;i++)
- {
- cin>>num;
- mp[num]++;//incrementing the frequency of the given element
- }
- for(auto j:mp)//traversing the map
- {
- if(mx<j.second)//checking if the frequency of the present key is greater than the previous greatest one
- {
- ans=j.first;
- mx=j.second;
- }
- }
- cout<<"Most frequent element: "<<ans<<endl;
- cout<<"Frequency: "<<mx<<endl;
- }
Add Comment
Please, Sign In to add comment