Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <array>
- using namespace std;
- const int maxSize = 1000;
- array<int, maxSize> readInput(int& actualSize) {
- array<int, maxSize> arr{};
- cin >> actualSize;
- for (int i = 0; i < actualSize; ++i) {
- cin >> arr[i];
- }
- return arr;
- }
- int mostFrequent(array<int, maxSize> arr, int n)
- {
- int max_count = 1, res = arr[0], curr_count = 1;
- for (int i = 1; i < n; i++) {
- if (arr[i] == arr[i - 1])
- curr_count++;
- else {
- if (curr_count > max_count) {
- max_count = curr_count;
- res = arr[i - 1];
- }
- curr_count = 1;
- }
- }
- if (curr_count > max_count)
- {
- max_count = curr_count;
- res = arr[n - 1];
- }
- return res;
- }
- int main()
- {
- int arrSize = 0;
- array<int, maxSize> arr = readInput(arrSize);
- cout << mostFrequent(arr, arrSize);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment