fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <limits>
  5. int main() {
  6. using namespace std;
  7.  
  8. int n;
  9. cout<<"Hello! How many numbers will you be entering?";
  10. cin>>n;
  11.  
  12. if(n<=0){
  13. cout<<"Looks like there are no numbers to check. Goodbye!"<<endl;
  14. return 0;}
  15. vector<int> arr(n);
  16. cout <<"Great, please enter your "<<n<<" numbers: "<<endl;
  17. for (int i=0;i<n;++i) {
  18. cin >> arr[i];
  19. }
  20. unordered_map<int, int> freqMap;
  21. for (int number : arr) {
  22. freqMap[number]++;
  23. }
  24. int maxFreq = 0;
  25. int maxElement = 0;
  26. int minFreq = numeric_limits<int>::max();
  27. int minElement = 0;
  28. for (auto const& [element, frequency] : freqMap) {
  29. if (frequency>maxFreq) {
  30. maxFreq=frequency;
  31. maxElement=element;
  32. }
  33. if (frequency<minFreq) {
  34. minFreq=frequency;
  35. minElement=element;
  36. }
  37. }
  38. cout <<"\n--- All done! Here's what I found ---"<<endl;
  39. cout <<"The most frequent number is "<<maxElement<<", which appeared "<<maxFreq<<" times."<<endl;
  40. cout <<"The least frequent number is "<<minElement<<", which appeared "<<minFreq<<" times."<<endl;
  41. return 0;
  42. }
  43.  
  44.  
Success #stdin #stdout 0.01s 5312KB
stdin
5
12
12
12
11
11
stdout
Hello! How many numbers will you be entering?Great, please enter your 5 numbers: 

--- All done! Here's what I found ---
The most frequent number is 12, which appeared 3 times.
The least frequent number is 11, which appeared 2 times.