fork download
  1. #include <iostream>
  2. #include <climits>
  3. using namespace std;
  4.  
  5. int main() {
  6. int n;
  7. cout << "Enter number of elements: ";
  8. cin >> n;
  9.  
  10. int arr[100]; // assuming max size 100
  11.  
  12. for(int i = 0; i < n; i++) {
  13. cin >> arr[i];
  14. }
  15.  
  16. int maxElement = arr[0], minElement = arr[0];
  17. int maxCount = 0, minCount = INT_MAX;
  18.  
  19. for(int i = 0; i < n; i++) {
  20. int cnt = 0;
  21.  
  22. for(int j = 0; j < n; j++) {
  23. if(arr[i] == arr[j]) {
  24. cnt++;
  25. }
  26. }
  27.  
  28. if(cnt > maxCount) {
  29. maxCount = cnt;
  30. maxElement = arr[i];
  31. }
  32.  
  33. if(cnt < minCount) {
  34. minCount = cnt;
  35. minElement = arr[i];
  36. }
  37. }
  38.  
  39. cout << "Element " << maxElement
  40. << " has maximum freq " << maxCount << endl;
  41.  
  42. cout << "Element " << minElement
  43. << " has minimum freq " << minCount << endl;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5276KB
stdin
6
2 2 5 2 4 4
stdout
Enter number of elements: Element 2 has maximum freq 3
Element 5 has minimum freq 1