Write a C function called maxfrequency which takes as argume
Write a C++ function called max_frequency which takes as arguments, integer x and returns the frequency of the digits in x, using a single one dimensional array. In case of a tie, the function should return the largest digit. E.g. for the number 3136569, both 3 and 6 appear twice but the function returns 6
Solution
Here is the program for given question
#include <iostream>
 using namespace std;
 int max_frequency(int x)
 {
    int n=x,digit,max_count,max_digit=0;
    //the counters for digits 0 to 9 are stored in array, so counts[0] is the number of times 0 appears, counts[1] is number of times 1 appears
    int counts[10]={0,0,0,0,0,0,0,0,0,0};
   
   
    //extract rightmost digit by getting remainder of division by 10
    while(n>0)
    {
        digit=n%10;
        n/=10;
        counts[digit]=counts[digit]+1;
        //cout<<\"digit = \"<<digit<<\"count \"<<counts[digit]<<endl;
        if(counts[digit]>counts[max_digit]) //check if count of the current digit is greater than the count of the previous max_digit
        {
            max_digit=digit;
            max_count=counts[digit];
        }
        else if(counts[digit]==counts[max_digit]) //if the current\'s digits count is same as previous max count, then save the digit which is larger in value
        {
            if(digit>max_digit)
                max_digit=digit;
        }      
    }
    return max_digit;
 }
 int main()
 {
    long num;
    cout<<\"Enter a number : \";
    cin>>num;
    cout<<\"Highest frequency digit is \"<<max_frequency(num)<<endl;
    return 0;
 }
output
Enter a number : 3136569
 Highest frequency digit is 6
Enter a number : 35224242
 Highest frequency digit is 2

