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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find k most frequent in linear time
- Program to find second most frequent character
- Find top k (or most frequent) numbers in a stream
- Find the remainder when First digit of a number is divided by its Last digit
- Find the Number which contain the digit d
- Find last digit in factorial
- Find the last digit of given series
- Find two numbers with sum N such that neither of them contains digit K
- Find the middle digit of a given Number
- Program to find the last digit of X in base Y
- Find the occurrences of digit d in the range [0..n]
- Find Last Digit of a^b for Large Numbers
- Find the largest N digit multiple of N
- Find nth number that contains the digit k or divisible by k.
- Program to find last digit of n'th Fibonnaci Number
- Find a N-digit number such that it is not divisible by any of its digits
- Find minimum possible digit sum after adding a number d
- Find the Nth digit in the proper fraction of two numbers
- Find the unit place digit of sum of N factorials
- Find unit digit of x raised to power y