beginnerDigit ProblemsJava

Find the Frequency of Each Digit in a Number

Count how many times each digit 0-9 occurs in an integer using arithmetic only.

Quick Answer

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

Example 1
Input:  112233
Output: 1->2, 2->2, 3->2

Each of the digits 1, 2 and 3 occurs exactly twice.

Example 2
Input:  1000
Output: 0->3, 1->1

The 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 0 be if the input itself is 0?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
You only ever deal with digits 0 through 9, so a fixed array of length 10 can hold every possible count.
Hint 2
Use `count[n % 10]++` to tally the last digit, then `n = n / 10` to move on to the next one.
Hint 3
After the loop, walk the array from index 0 to 9 and print only the indices whose count is greater than 0.

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.

  1. Create int[] count = new int[10] (all zeros by default).
  2. While n is not 0:
    • digit = n % 10 — the current last digit.
    • count[digit]++ — record one more occurrence.
    • n = n / 10 — drop the digit you just counted.
  3. Walk count from 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.

Time: O(d) where d is the number of digits (log10 n)Space: O(1) — the count array is always exactly 10 slots

Common 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 →
Chat with us