fork download
  1. // We are given an Array of Numbers. //We have to find and print any Number with Maximum Frequency and Minimum Frequency. //Arr = [3, 2, 3, 2, 4, 3]
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <vector>
  5. using namespace std;
  6. int main()
  7. { vector<int> arr={3, 2, 3, 2, 4, 3,2,2,2,2,2};
  8. unordered_map<int,int> hashmap;
  9. for (int i=0;i<arr.size();i++)
  10.  
  11. {
  12. hashmap[arr[i]]=hashmap[arr[i]]+1;
  13. }
  14. // Count frequencies for (int num : arr) { freq[num]++;}
  15.  
  16. int maxFreq = -1, minFreq = arr.size()+1;
  17. int maxNum = -1, minNum = -1;
  18. for (auto &p : hashmap)
  19. {
  20. if (p.second > maxFreq) {
  21. maxFreq = p.second;
  22. maxNum = p.first;
  23. }
  24. if (p.second < minFreq) {
  25. minFreq = p.second;
  26. minNum = p.first;
  27. }
  28. }
  29. cout<<maxNum<<"\n"<< minNum;
  30. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2
4