fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n = 6;
  6. vector<int> arr = {1, 1, 2, 3, 3, 3};
  7.  
  8. unordered_map<int, int> mp;
  9. int maxiFreq = INT_MIN, maxiElement = arr[0];
  10. int miniFreq = INT_MAX, miniElement = arr[0];
  11.  
  12. // Count frequency of each element
  13. for (int i = 0; i < arr.size(); i++) {
  14. mp[arr[i]]++;
  15. }
  16.  
  17. // Find max and min frequency elements
  18. for (auto num : mp) {
  19. if (num.second >= maxiFreq) {
  20. maxiFreq = num.second;
  21. maxiElement = num.first;
  22. }
  23. if (num.second <= miniFreq) {
  24. miniFreq = num.second;
  25. miniElement = num.first;
  26. }
  27. }
  28.  
  29. cout << "Max frequency element: " << maxiElement
  30. << " with frequency: " << maxiFreq << endl;
  31.  
  32. cout << "Min frequency element: " << miniElement
  33. << " with frequency: " << miniFreq << endl;
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Max frequency element: 3 with frequency: 3
Min frequency element: 2 with frequency: 1