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. int maxFreq = -1, minFreq = arr.size()+1;
  14. int maxNum = -1, minNum = -1;
  15. for (auto &p : hashmap)
  16. {
  17. if (p.second > maxFreq) {
  18. maxFreq = p.second;
  19. maxNum = p.first;
  20. }
  21. if (p.second < minFreq) {
  22. minFreq = p.second;
  23. minNum = p.first;
  24. }
  25. }
  26. cout<<maxNum<<"\n"<< minNum;
  27. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
2
4