mstoyanov7

Untitled

Apr 10th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4. using namespace std;
  5.  
  6. const int maxSize = 1000;
  7.  
  8. array<int, maxSize> readInput(int& actualSize) {
  9. array<int, maxSize> arr{};
  10. cin >> actualSize;
  11.  
  12. for (int i = 0; i < actualSize; ++i) {
  13. cin >> arr[i];
  14. }
  15. return arr;
  16. }
  17.  
  18. int mostFrequent(array<int, maxSize> arr, int n)
  19. {
  20.  
  21. int max_count = 1, res = arr[0], curr_count = 1;
  22. for (int i = 1; i < n; i++) {
  23. if (arr[i] == arr[i - 1])
  24. curr_count++;
  25. else {
  26. if (curr_count > max_count) {
  27. max_count = curr_count;
  28. res = arr[i - 1];
  29. }
  30. curr_count = 1;
  31. }
  32. }
  33.  
  34. if (curr_count > max_count)
  35. {
  36. max_count = curr_count;
  37. res = arr[n - 1];
  38. }
  39.  
  40. return res;
  41. }
  42.  
  43. int main()
  44. {
  45. int arrSize = 0;
  46. array<int, maxSize> arr = readInput(arrSize);
  47. cout << mostFrequent(arr, arrSize);
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment