Advertisement
Garey

Most occuring digit

Dec 20th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int max_occurrences(int [], size_t);
  7. int digitOccurence(long int number, int digit);
  8.  
  9. int main() {
  10.  
  11.     int array[] = { 1, 2, 3, 4, 5, 6, 6 };
  12.  
  13.     cout << endl << max_occurrences(array, 7) << endl;
  14.  
  15.     return 0;
  16. }
  17.  
  18. int max_occurrences(int array[], size_t array_size) {
  19.     string str;
  20.     string::size_type sz;
  21.  
  22.     size_t max_count = 1, result = 0;
  23.  
  24.     for (size_t i = 0; i < array_size; i++) {
  25.         str += to_string(array[i]);
  26.     }
  27.    
  28.     long number = stol(str, &sz);
  29.  
  30.     cout << number << endl;
  31.  
  32.     for (size_t i = 0; i < 10; i++) {
  33.         int count = digitOccurence(number, i);
  34.  
  35.         if (count >= max_count) {
  36.             max_count = count;
  37.             result = i;
  38.         }
  39.     }
  40.  
  41.     return result;
  42. }
  43.  
  44. int digitOccurence(long int number, int digit) {
  45.     int count = 0;
  46.     while (number) {
  47.         if (number % 10 == digit)
  48.             count++;
  49.  
  50.         number /= 10;
  51.     }
  52.  
  53.     return count;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement