Find the most frequent digit without using array/string

29 Jun, 2022
Given an integer, find the most occurring digit in it. If two or more digits occur same number of times, then return the highest of them. Input integer is given as an int variable, not as a string or array. Use of hash or array or string is not allowed. 
Example: 
Input:  x = 12234
Output: The most frequent digit is 2

Input:  x = 1223377
Output: The most frequent digit is 7

Input:  x = 5
Output: The most frequent digit is 5

Input:  x = 1000
Output: The most frequent digit is 0
We strongly recommend you to minimize your browser and try this yourself first. 
We could create a map of size 10 and store count of all digits, but use of any array/string is not allowed.The idea is simple, we write a function that counts occurrences of a given digit in a given integer. Then we count all digits from 0 to 9 in given integer. We keep updating maximum count whenever count becomes more or same as previous count. Below is the implementation.
Implementation:
C++
Java
Python3
C#
PHP
Javascript
// Finds maximum occurring digit without using any array/string#include <bits/stdc++.h>using namespace std; // Simple function to count occurrences of digit d in xint countOccurrences(long int x, int d){    int count = 0;  // Initialize count of digit d    while (x)    {        // Increment count if current digit is same as d        if (x%10 == d)           count++;        x = x/10;    }    return count;} // Returns the max occurring digit in xint maxOccurring(long int x){   // Handle negative number   if (x < 0)      x = -x;    int result = 0; // Initialize result which is a digit   int max_count = 1; // Initialize count of result    // Traverse through all digits   for (int d=0; d<=9; d++)   {      // Count occurrences of current digit      int count = countOccurrences(x, d);       // Update max_count and result if needed      if (count >= max_count)      {         max_count = count;         result = d;      }   }   return result;} // Driver programint main(){    long int x = 1223355;    cout << "Max occurring digit is " << maxOccurring(x);    return 0;}
// Finds maximum occurring digit// without using any array/stringimport java.io.*; class GFG{     // Simple function to count// occurrences of digit d in xstatic int countOccurrences(int x,                            int d){    // Initialize count    // of digit d    int count = 0;    while (x > 0)    {        // Increment count if        // current digit is        // same as d        if (x % 10 == d)        count++;        x = x / 10;    }    return count;} // Returns the max// occurring digit in xstatic int maxOccurring( int x){     // Handle negative numberif (x < 0)    x = -x; // Initialize result// which is a digitint result = 0; // Initialize count// of resultint max_count = 1; // Traverse through// all digitsfor (int d = 0; d <= 9; d++){    // Count occurrences    // of current digit    int count = countOccurrences(x, d);     // Update max_count    // and result if needed    if (count >= max_count)    {        max_count = count;        result = d;    }}return result;} // Driver Codepublic static void main (String[] args){    int x = 1223355;    System.out.println("Max occurring digit is " +                                 maxOccurring(x));     }} // This code is contributed// by akt_mit
# Finds maximum occurring digit# without using any array/string # Simple function to count# occurrences of digit d in xdef countOccurrences(x, d):    count = 0; # Initialize count               # of digit d    while (x):                 # Increment count if current        # digit is same as d        if (x % 10 == d):            count += 1;        x = int(x / 10);     return count; # Returns the max occurring# digit in xdef maxOccurring(x):         # Handle negative number    if (x < 0):        x = -x;         result = 0; # Initialize result                # which is a digit    max_count = 1; # Initialize count                   # of result         # Traverse through all digits    for d in range(10):                 # Count occurrences of current digit        count = countOccurrences(x, d);                 # Update max_count and        # result if needed        if (count >= max_count):            max_count = count;            result = d;             return result; # Driver Codex = 1223355;print("Max occurring digit is",              maxOccurring(x)); # This code is contributed by mits.
// Finds maximum occurring digit// without using any array/stringclass GFG{     // Simple function to count// occurrences of digit d in xstatic int countOccurrences(int x, int d){    // Initialize count    // of digit d    int count = 0;    while (x > 0)    {        // Increment count if        // current digit is        // same as d        if (x % 10 == d)        count++;        x = x / 10;    }    return count;} // Returns the max// occurring digit in xstatic int maxOccurring( int x){     // Handle negative numberif (x < 0)    x = -x; // Initialize result// which is a digitint result = 0; // Initialize count// of resultint max_count = 1; // Traverse through// all digitsfor (int d = 0; d <= 9; d++){    // Count occurrences    // of current digit    int count = countOccurrences(x, d);     // Update max_count    // and result if needed    if (count >= max_count)    {        max_count = count;        result = d;    }}return result;} // Driver Codestatic void Main(){    int x = 1223355;    System.Console.WriteLine("Max occurring digit is " +                                       maxOccurring(x));}} // This code is contributed by mits
<?php// Finds maximum occurring digit// without using any array/string // Simple function to count// occurrences of digit d in xfunction countOccurrences($x, $d){    // Initialize count of digit d    $count = 0;    while ($x)    {        // Increment count if current        // digit is same as d        if ($x % 10 == $d)        $count++;        $x = (int)($x / 10);    }    return $count;} // Returns the max occurring// digit in xfunction maxOccurring($x){// Handle negative numberif ($x < 0)    $x = -$x; $result = 0; // Initialize result             // which is a digit$max_count = 1; // Initialize count of result // Traverse through all digitsfor ($d = 0; $d <= 9; $d++){    // Count occurrences of    // current digit    $count = countOccurrences($x, $d);     // Update max_count and result    // if needed    if ($count >= $max_count)    {        $max_count = $count;        $result = $d;    }}return $result;} // Driver Code$x = 1223355;echo "Max occurring digit is " .               maxOccurring($x); // This code is contributed by mits?>
<script> // Finds maximum occurring digit// without using any array/string      // Simple function to count// occurrences of digit d in xfunction countOccurrences(x, d){    // Initialize count    // of digit d    var count = 0;    while (x > 0)    {        // Increment count if        // current digit is        // same as d        if (x % 10 == d)        count++;        x = parseInt(x / 10);    }    return count;} // Returns the max// occurring digit in xfunction maxOccurring(x){         // Handle negative number    if (x < 0)        x = -x;         // Initialize result    // which is a digit    var result = 0;         // Initialize count    // of result    var max_count = 1;         // Traverse through    // all digits    for (d = 0; d <= 9; d++)    {        // Count occurrences        // of current digit        var count = countOccurrences(x, d);             // Update max_count        // and result if needed        if (count >= max_count)        {            max_count = count;            result = d;        }    }    return result;} // Driver Codevar x = 1223355;document.write("Max occurring digit is " +                             maxOccurring(x)); // This code contributed by shikhasingrajput </script>
Max occurring digit is 5
Time Complexity: O(log10x)Auxiliary Space: O(1)
jit_t
Mithun Kumar
shikhasingrajput
samim2000
hardikkoriintern
Hash
Mathematical
Strings
Hash
Strings
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Hashing | A Complete Tutorial
Longest Consecutive Subsequence
Counting frequencies of array elements
Hashing | Set 2 (Separate Chaining)
Sorting a Map by value in C++ STL
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays