Guest User

04. MostFrequentNumber

a guest
Aug 31st, 2020
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <iomanip>
  5. #include <ctime>
  6. #include <limits>
  7. #include <vector>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11.  
  12. void fillArr(vector <int>& arr);
  13. void mostFrequentNumber(vector <int>& arr);
  14.  
  15.  
  16. int main() {
  17.     int lenghtArr{};
  18.     cin >> lenghtArr;
  19.     vector <int> arr(lenghtArr);
  20.  
  21.     fillArr(arr);
  22.     mostFrequentNumber(arr);
  23.  
  24.     return 0;
  25. }
  26.  
  27. void fillArr(vector <int>& arr) {
  28.     int num{};
  29.     for (int i = 0; i < arr.size(); i++) {
  30.         cin >> arr[i];
  31.     }
  32. }
  33.  
  34. void mostFrequentNumber(vector <int>& arr) {
  35.  
  36.     vector <int> reppArr = arr;
  37.     reppArr.erase(reppArr.begin(), reppArr.end());
  38.     int currentNum{},biggestNum{},numPlusNext{},bigestCounter{},currCounter{},counter{},num{};
  39.  
  40.     for (int i = 0; i < arr.size(); i++) {
  41.         numPlusNext = arr[i + 1];
  42.         currentNum = arr[i];
  43.  
  44.     if (currentNum == numPlusNext) {
  45.         counter++;
  46.         currCounter += counter;
  47.         num = numPlusNext;
  48.         }
  49.     else {
  50.         currCounter = 0;
  51.         }
  52.  
  53.  
  54.     if (currCounter > bigestCounter) {
  55.         bigestCounter = currCounter;
  56.         biggestNum = num;
  57.         }
  58.  
  59.     if (currCounter == bigestCounter) {
  60.         auto it = find(reppArr.begin(), reppArr.end(), num);
  61.         if (it == reppArr.end()) {      // element not found
  62.         reppArr.push_back(num);
  63.         }
  64.         }
  65.  
  66.  
  67.     if (arr[i + 2] == arr.size()) {
  68.             arr.erase(arr.begin());
  69.         }
  70.     }
  71.  
  72.  
  73.  
  74.     for(int i = 0; i < reppArr.size();i++){
  75.  
  76.     if(reppArr[0] !=0){
  77.         sort(reppArr.begin(),reppArr.end());
  78.  
  79.     if(reppArr[i] == reppArr.back()){
  80.         cout << reppArr[i];
  81.     } else{
  82.         cout << reppArr[i] << " ";
  83.     }
  84.  
  85.     }else{
  86.          cout << biggestNum << '\n';
  87.          break;
  88.     }
  89.  
  90.     }
  91.             }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
RAW Paste Data