fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7. int v[n] = {};
  8. int freq[n] = {};
  9. for(int i = 0; i < n; i++)
  10. {
  11. cin >> v[i];
  12. freq[v[i]]++;
  13. }
  14.  
  15. int mx = 0;
  16. int index = 0; //as there will be always a majority number
  17. //so then no worry cause the index value will always change
  18. //and if it stay zero then the majority element is zero
  19. for(int i = 0; i < n; i++)
  20. {
  21. if(mx != max(mx , freq[i]))
  22. {
  23. mx = max(mx , freq[i]);
  24. index = i;
  25. }
  26. }
  27. cout << index << endl;
  28. return 0;
  29. }
Success #stdin #stdout 0s 5316KB
stdin
5
1 2 2 4 2
stdout
2