Adrita

ds lab 8 task 2

Jul 28th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int m;
  6. cin>>m;
  7. if(m<=0||m>=100)
  8. {
  9. cout<<"Invalid input"<<endl;//checking if m satisfies the conditions
  10. return 0;
  11. }
  12. int num,i,mx=0,ans=0;
  13. unordered_map<int,int>mp;
  14. for(i=0;i<m;i++)
  15. {
  16. cin>>num;
  17. mp[num]++;//incrementing the frequency of the given element
  18. }
  19. for(auto j:mp)//traversing the map
  20. {
  21. if(mx<j.second)//checking if the frequency of the present key is greater than the previous greatest one
  22. {
  23. ans=j.first;
  24. mx=j.second;
  25. }
  26. }
  27. cout<<"Most frequent element: "<<ans<<endl;
  28. cout<<"Frequency: "<<mx<<endl;
  29. }
  30.  
Add Comment
Please, Sign In to add comment