Find the Frequency of Each Digit in a Number
Count how many times each digit 0-9 occurs in an integer using arithmetic only.
Create an int array of size 10 that acts as a tally for digits 0 through 9. Peel off the last digit with number % 10, increment count[digit], then drop the digit with number / 10. Repeat until the number is 0, then read off the array for the frequency of each digit.
Problem Statement
Given a non-negative integer, report how many times each digit (0-9)
appears in it. You should not convert the number to a String; use arithmetic
only.
For example, in 112233 the digit 1 appears twice, 2 appears twice and 3
appears twice. Report only the digits that actually occur, along with their
counts.
Input: A single non-negative integer n.
Output: For each digit that appears in n, the digit and how many times it occurs.
Examples
Input: 112233
Output: 1->2, 2->2, 3->2Each of the digits 1, 2 and 3 occurs exactly twice.
Input: 1000
Output: 0->3, 1->1The three trailing zeros give 0 a count of 3; the leading 1 occurs once.
Constraints
0 <= n <= 2,147,483,647 (fits in a 32-bit int)No String conversion; arithmetic only
Think Before You Code
Reveal the questions to ask yourself first
- There are only 10 possible digits — what data structure fits that exactly?
- How do you read the last digit of a number without a String?
- What should the frequency of a lone
0be if the input itself is0?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
A digit is always one of ten values, so an array of length 10 is the perfect
tally sheet — index d stores how many times digit d appeared.
- Create
int[] count = new int[10](all zeros by default). - While
nis not 0:digit = n % 10— the current last digit.count[digit]++— record one more occurrence.n = n / 10— drop the digit you just counted.
- Walk
countfrom index 0 to 9 and report every index whose value is above 0.
Because the tally is indexed by the digit itself, the array comes out already sorted by digit value.
Dry Run
Walk through the example step by step
Counting digits of n = 112233:
step | n | digit = n%10 | count[] after
-----+--------+--------------+---------------------------
1 | 112233 | 3 | [.,.,.,1,...]
2 | 11223 | 3 | [.,.,.,2,...]
3 | 1122 | 2 | [.,.,2,2,...]
4 | 112 | 2 | [.,.,2,2,...] -> 2 count = 2
5 | 11 | 1 | 1 count = 1
6 | 1 | 1 | 1 count = 2
end | 0 | — | 1->2, 2->2, 3->2
Solution
Reveal the full Java solution
public class DigitFrequency {
public static int[] frequency(int n) {
int[] count = new int[10];
if (n == 0) {
count[0] = 1;
return count;
}
while (n != 0) {
count[n % 10]++;
n /= 10;
}
return count;
}
public static void main(String[] args) {
int[] a = frequency(112233);
for (int d = 0; d < 10; d++) {
if (a[d] > 0) System.out.println(d + " -> " + a[d]);
}
System.out.println("---");
int[] b = frequency(1000);
for (int d = 0; d < 10; d++) {
if (b[d] > 0) System.out.println(d + " -> " + b[d]);
}
// Prints:
// 1 -> 2
// 2 -> 2
// 3 -> 2
// ---
// 0 -> 3
// 1 -> 1
}
}
The count array turns digit counting into direct indexing: the digit value is
the position, so no comparisons or searching are needed. The special case for
n == 0 matters because the while loop would never run for zero, yet the
number does contain one digit — a 0.
O(d) where d is the number of digits (log10 n)Space: O(1) — the count array is always exactly 10 slotsCommon Mistakes
- Returning an empty result for n = 0 because the while loop body never executes.
- Using a HashMap when a fixed 10-element array is simpler and faster for digits.
Edge Cases to Test
- n = 0 should report the digit 0 with a count of 1.
- Numbers with repeated trailing zeros such as 1000 give 0 a large count.
- A single-digit number like 7 reports just 7->1.
Interview Follow-Ups
- How would you find the digit that occurs most frequently?
- How would you extend this to count digit frequency across an entire array of numbers?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →