fork download
  1. #include<bits/stdc++.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8. vector <int> arr={3, 2, 3, 2, 4, 3};
  9.  
  10. map<int,int>mp;
  11. for(int i=0;i<arr.size();i++){
  12. mp[arr[i]]++;
  13.  
  14. }
  15.  
  16. for(auto i:mp){
  17. cout<<"Element "<<i.first<<" has "<<i.second<<" repetition"<<endl;
  18. }
  19.  
  20. int maxFreq=0;
  21. int maxElement=0;
  22. int minFreq=arr.size()+1;
  23. int minElement=0;
  24. for(auto i:mp){
  25. if(i.second>maxFreq){
  26. maxFreq=i.second;
  27. maxElement=i.first;
  28. }
  29. if(i.second<minFreq){
  30. minFreq=i.second;
  31. minElement=i.first;
  32. }
  33.  
  34. }
  35.  
  36. cout<<"Maximum freq of "<<maxElement<<" with freq "<<maxFreq<<endl;
  37. cout<<"Minimum freq of "<<minElement<<" with freq "<<minFreq<<endl;
  38.  
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Element 2 has 2 repetition
Element 3 has 3 repetition
Element 4 has 1 repetition
Maximum freq of 3 with freq 3
Minimum freq of 4 with freq 1