beginnerDigit ProblemsJava

Count the Occurrences of a Given Digit in a Number

Count how many times a specific digit appears in an integer, using arithmetic only.

Quick Answer

Peel off the last digit with number % 10 and compare it to the target digit; increment a counter on a match. Drop the digit with number / 10 and repeat until the number is 0. The counter is how many times the target digit appears.

Problem Statement

Given a non-negative integer n and a target digit d (0-9), count how many times d appears in n. Do not convert the number to a String; use arithmetic only.

For example, in 122324 the digit 2 appears 3 times. Take care with 0: the number 100 contains the digit 0 twice.

Input: A non-negative integer n and a target digit d in the range 0-9.

Output: The number of times d occurs among the digits of n.

Examples

Example 1
Input:  n = 122324, d = 2
Output: 3

The digits are 1, 2, 2, 3, 2, 4 — the digit 2 appears three times.

Example 2
Input:  n = 100, d = 0
Output: 2

The digits are 1, 0, 0 — the digit 0 appears twice.

Constraints

  • 0 <= n <= 2,147,483,647 (fits in a 32-bit int)
  • 0 <= d <= 9
  • No String conversion; arithmetic only

Think Before You Code

Reveal the questions to ask yourself first
  • How do you look at one digit of the number at a time?
  • What comparison tells you the current digit matches the target?
  • If the target digit is 0, does your loop still handle the input 0 correctly?

Hints

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

Hint 1
Use `n % 10` to read the last digit and `n / 10` to remove it.
Hint 2
Increment a counter whenever the extracted digit equals the target digit.
Hint 3
The number 0 has a single digit — 0 — so handle it before the loop if the target is 0.

Approach

Reveal the step-by-step approach

Scan the number digit by digit from the right, counting matches.

  1. Set count = 0.
  2. If n == 0, it still contains one digit (0), so return 1 when d == 0, else 0.
  3. While n is not 0:
    • digit = n % 10.
    • If digit == d, increment count.
    • n = n / 10.
  4. Return count.

The explicit n == 0 handling is what makes counting the digit 0 correct for the input 0 itself.

Dry Run

Walk through the example step by step

Counting the digit 2 in n = 122324:

step | n      | digit | match? | count
-----+--------+-------+--------+------
1    | 122324 | 4     | no     | 0
2    | 12232  | 2     | yes    | 1
3    | 1223   | 3     | no     | 1
4    | 122    | 2     | yes    | 2
5    | 12     | 2     | yes    | 3
6    | 1      | 1     | no     | 3
end  | 0      | —     | —      | 3

Solution

Reveal the full Java solution
public class CountDigit {
    public static int countOccurrences(int n, int d) {
        if (n == 0) return d == 0 ? 1 : 0;
        int count = 0;
        while (n != 0) {
            if (n % 10 == d) count++;
            n /= 10;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(countOccurrences(122324, 2)); // 3
        System.out.println(countOccurrences(100, 0));     // 2
    }
}

Each iteration inspects exactly one digit and the counter records matches, so the method is a single linear pass. The n == 0 guard is the one subtlety: the loop would otherwise never run, incorrectly reporting zero occurrences of the digit 0 for the input 0.

Time: O(d) where d is the number of digitsSpace: O(1)

Common Mistakes

  • Reporting 0 for countOccurrences(0, 0) because the while loop never executes.
  • Comparing against the character '2' instead of the integer 2 when adapting String-based code.

Edge Cases to Test

  • n = 0 with target 0 returns 1; with any other target returns 0.
  • A digit that never appears returns 0.
  • Numbers where every digit matches, like counting 5 in 555, return the digit count.

Interview Follow-Ups

  • How would you return the frequency of all ten digits in a single pass?
  • How would you find which digit occurs most often?

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