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.
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.
Max occurring digit is 5
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to find second most frequent character
- Find top k (or most frequent) numbers in a stream
- Find k most frequent in linear time
- Most frequent word in an array of strings
- Smallest subarray with all occurrences of a most frequent element
- Most frequent element in an array
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Most frequent word in first String which is not present in second String
- Most frequent factor in a range of integers
- Kth most frequent Character in a given String
- Most frequent element in Array after replacing given index by K for Q queries
- Count of N-digit numbers having digit XOR as single digit
- Find the minimum number possible by changing at most one digit
- Find the greater number closest to N having at most one non-zero digit
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Find the remainder when First digit of a number is divided by its Last digit
- Check if a string can be split into two strings with same number of K-frequent characters
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Right most non-zero digit in multiplication of array elements
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's