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++
#include <bits/stdc++.h>
using namespace std;
int countOccurrences( long int x, int d)
{
int count = 0;
while (x)
{
if (x%10 == d)
count++;
x = x/10;
}
return count;
}
int maxOccurring( long int x)
{
if (x < 0)
x = -x;
int result = 0;
int max_count = 1;
for ( int d=0; d<=9; d++)
{
int count = countOccurrences(x, d);
if (count >= max_count)
{
max_count = count;
result = d;
}
}
return result;
}
int main()
{
long int x = 1223355;
cout << "Max occurring digit is " << maxOccurring(x);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countOccurrences( int x,
int d)
{
int count = 0 ;
while (x > 0 )
{
if (x % 10 == d)
count++;
x = x / 10 ;
}
return count;
}
static int maxOccurring( int x)
{
if (x < 0 )
x = -x;
int result = 0 ;
int max_count = 1 ;
for ( int d = 0 ; d <= 9 ; d++)
{
int count = countOccurrences(x, d);
if (count >= max_count)
{
max_count = count;
result = d;
}
}
return result;
}
public static void main (String[] args)
{
int x = 1223355 ;
System.out.println( "Max occurring digit is " +
maxOccurring(x));
}
}
|
Python3
def countOccurrences(x, d):
count = 0 ;
while (x):
if (x % 10 = = d):
count + = 1 ;
x = int (x / 10 );
return count;
def maxOccurring(x):
if (x < 0 ):
x = - x;
result = 0 ;
max_count = 1 ;
for d in range ( 10 ):
count = countOccurrences(x, d);
if (count > = max_count):
max_count = count;
result = d;
return result;
x = 1223355 ;
print ( "Max occurring digit is" ,
maxOccurring(x));
|
C#
class GFG
{
static int countOccurrences( int x, int d)
{
int count = 0;
while (x > 0)
{
if (x % 10 == d)
count++;
x = x / 10;
}
return count;
}
static int maxOccurring( int x)
{
if (x < 0)
x = -x;
int result = 0;
int max_count = 1;
for ( int d = 0; d <= 9; d++)
{
int count = countOccurrences(x, d);
if (count >= max_count)
{
max_count = count;
result = d;
}
}
return result;
}
static void Main()
{
int x = 1223355;
System.Console.WriteLine( "Max occurring digit is " +
maxOccurring(x));
}
}
|
Javascript
<script>
function countOccurrences(x, d)
{
var count = 0;
while (x > 0)
{
if (x % 10 == d)
count++;
x = parseInt(x / 10);
}
return count;
}
function maxOccurring(x)
{
if (x < 0)
x = -x;
var result = 0;
var max_count = 1;
for (d = 0; d <= 9; d++)
{
var count = countOccurrences(x, d);
if (count >= max_count)
{
max_count = count;
result = d;
}
}
return result;
}
var x = 1223355;
document.write( "Max occurring digit is " +
maxOccurring(x));
</script>
|
PHP
<?php
function countOccurrences( $x , $d )
{
$count = 0;
while ( $x )
{
if ( $x % 10 == $d )
$count ++;
$x = (int)( $x / 10);
}
return $count ;
}
function maxOccurring( $x )
{
if ( $x < 0)
$x = - $x ;
$result = 0;
$max_count = 1;
for ( $d = 0; $d <= 9; $d ++)
{
$count = countOccurrences( $x , $d );
if ( $count >= $max_count )
{
$max_count = $count ;
$result = $d ;
}
}
return $result ;
}
$x = 1223355;
echo "Max occurring digit is " .
maxOccurring( $x );
?>
|
Output
Max occurring digit is 5
Time Complexity: O(log10x)
Auxiliary Space: O(1)
Approach#2: using division and modulus operator.
Count the frequency of each digit using division and modulus operator.
Algorithm
1. Initialize a variable “max_freq” to store the maximum frequency of any digit.
2. Initialize a variable “most_frequent_digit” to store the most frequent digit.
3. Repeat the following steps for each digit in the input number:
a. Initialize a variable “count” to 0.
b. Divide the input number by 10 repeatedly until it becomes 0 or less.
c. If the remainder of the division is equal to the current digit, increment “count”.
d. If “count” is greater than “max_freq”, update “max_freq” to “count” and “most_frequent_digit” to the current digit.
4. Return “most_frequent_digit”.
Java
public class GFG {
static int mostFrequentDigit( int x) {
int maxFreq = 0 ;
int mostFrequentDigit = x % 10 ;
while (x > 0 ) {
int digit = x % 10 ;
int count = 0 ;
int temp = x;
while (temp > 0 ) {
if (temp % 10 == digit) {
count++;
}
temp /= 10 ;
}
if (count > maxFreq) {
maxFreq = count;
mostFrequentDigit = digit;
}
x /= 10 ;
}
return mostFrequentDigit;
}
public static void main(String[] args) {
int x = 1223355 ;
System.out.println(mostFrequentDigit(x));
}
}
|
Python3
def most_frequent_digit(x):
max_freq = 0
most_frequent_digit = x % 10
while x > 0 :
digit = x % 10
count = 0
temp = x
while temp > 0 :
if temp % 10 = = digit:
count + = 1
temp / / = 10
if count > max_freq:
max_freq = count
most_frequent_digit = digit
x / / = 10
return most_frequent_digit
x = 1223355
print (most_frequent_digit(x))
|
Javascript
function most_frequent_digit(x) {
let max_freq = 0;
let most_frequent_digit = x % 10;
while (x > 0) {
let digit = x % 10;
let count = 0;
let temp = x;
while (temp > 0) {
if (temp % 10 == digit) {
count += 1;
}
temp = Math.floor(temp / 10);
}
if (count > max_freq) {
max_freq = count;
most_frequent_digit = digit;
}
x = Math.floor(x / 10);
}
return most_frequent_digit;
}
let x = 1223355;
console.log(most_frequent_digit(x));
|
Time complexity: O(d * log10(x)), where d is the number of digits in the input number.
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Aug, 2023
Like Article
Save Article