easyDigit ProblemsJava

Check Whether All Digits of a Number Are Distinct

Determine whether an integer contains any repeated digit, using arithmetic only.

Quick Answer

Keep a boolean array of size 10 marking which digits you have already seen. Extract each digit with number % 10; if it is already marked, the number has a duplicate and you return false. If you finish the loop without a collision, every digit was unique and you return true.

Problem Statement

Given a non-negative integer, decide whether all of its digits are distinct (no digit repeats). Return true if every digit is unique and false otherwise. Do not convert the number to a String; use arithmetic only.

For example, 1234 has all distinct digits, while 1223 repeats the digit 2.

Input: A single non-negative integer n.

Output: true if all digits of n are distinct, otherwise false.

Examples

Example 1
Input:  1234
Output: true

The digits 1, 2, 3, 4 are all different.

Example 2
Input:  1223
Output: false

The digit 2 appears twice, so the digits are not all distinct.

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
  • How can you remember which digits you have already encountered?
  • There are only ten possible digits — what size of flag array covers them all?
  • As soon as you find a repeat, do you need to keep scanning?

Hints

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

Hint 1
A boolean array of length 10 can flag whether each digit has been seen.
Hint 2
For each digit `d = n % 10`, if `seen[d]` is already true you have found a duplicate.
Hint 3
Return false the moment a duplicate is found; if the loop completes, return true.

Approach

Reveal the step-by-step approach

Track which digits have appeared using a boolean array indexed by the digit value.

  1. Create boolean[] seen = new boolean[10].
  2. While n is not 0:
    • digit = n % 10.
    • If seen[digit] is true, return false — a repeat.
    • Otherwise set seen[digit] = true.
    • n = n / 10.
  3. If the loop finishes, return true.

You can stop early on the first collision, so the scan does no unnecessary work.

Dry Run

Walk through the example step by step

Checking n = 1223:

step | n    | digit | seen[digit] before | action
-----+------+-------+--------------------+-------------------
1    | 1223 | 3     | false              | mark 3
2    | 122  | 2     | false              | mark 2
3    | 12   | 2     | true               | duplicate -> false

Solution

Reveal the full Java solution
public class DistinctDigits {
    public static boolean allDistinct(int n) {
        boolean[] seen = new boolean[10];
        if (n == 0) return true; // the single digit 0 is trivially distinct
        while (n != 0) {
            int digit = n % 10;
            if (seen[digit]) return false;
            seen[digit] = true;
            n /= 10;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(allDistinct(1234)); // true
        System.out.println(allDistinct(1223)); // false
    }
}

Indexing a boolean array by the digit value gives constant-time membership checks, so the whole scan is linear in the number of digits. Returning early on the first repeat keeps the method fast and clear.

Time: O(d) where d is the number of digitsSpace: O(1) — a fixed 10-element boolean array

Common Mistakes

  • Forgetting to mark a digit as seen before moving on, so duplicates are never detected.
  • Continuing to loop after a duplicate is found instead of returning immediately.

Edge Cases to Test

  • n = 0 is trivially distinct and returns true.
  • Any single-digit number returns true.
  • A number with a repeated leading and trailing digit, like 101, returns false.

Interview Follow-Ups

  • How would you return the first digit that repeats instead of a boolean?
  • How would you check distinctness ignoring one specific digit?

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