Number Theory
Mathematics
Distinct Digits
Counting
Math Problem Solving

Distinct digit count

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The distinct digit count of an integer is the number of different decimal digits that appear in it at least once. It is a small problem, but it shows up often in interview questions, digit-DP problems, and input-validation tasks where repeated digits matter.

Basic Idea

If the number is 112233, the distinct digits are 1, 2, and 3, so the count is 3. Repeated appearances do not change the answer; only whether a digit appears at all matters.

The most direct implementation is to inspect each digit and store it in a set:

python
1def distinct_digit_count(n: int) -> int:
2    digits = set(str(abs(n)))
3    return len(digits)
4
5
6print(distinct_digit_count(112233))   # 3
7print(distinct_digit_count(276547))   # 5
8print(distinct_digit_count(111555))   # 2

Using abs(n) makes negative numbers behave the same as their positive form, which is usually the intended interpretation.

A Numeric Approach Without Strings

If you want to solve it using arithmetic rather than string conversion, peel digits off with modulo and division:

python
1def distinct_digit_count_math(n: int) -> int:
2    n = abs(n)
3    if n == 0:
4        return 1
5
6    seen = set()
7    while n > 0:
8        seen.add(n % 10)
9        n //= 10
10
11    return len(seen)
12
13
14print(distinct_digit_count_math(902900))  # 3

This version is useful when the problem is part of a larger digit-based algorithm and you want to stay in integer arithmetic.

Bitmask Optimization

Because decimal digits are limited to 0 through 9, you can also track them with a bitmask instead of a set:

python
1def distinct_digit_count_mask(n: int) -> int:
2    n = abs(n)
3    if n == 0:
4        return 1
5
6    mask = 0
7    while n > 0:
8        digit = n % 10
9        mask |= 1 << digit
10        n //= 10
11
12    return mask.bit_count()
13
14
15print(distinct_digit_count_mask(1234512345))  # 5

This is more compact at the machine level and can be handy in competitive programming or state-compression problems.

Edge Cases Matter

The most important edge cases are:

  • '0, whose decimal representation contains one distinct digit: 0'
  • negative numbers, where the minus sign should not count as a digit
  • very large integers, where the logic still works but performance details may matter more

For example:

python
print(distinct_digit_count(0))       # 1
print(distinct_digit_count(-7070))   # 2
print(distinct_digit_count(1234567890))  # 10

The last case shows the maximum possible answer in base 10: ten distinct digits.

Where This Shows Up

Distinct digit counting is often part of bigger problems such as:

  • checking whether a number has all unique digits
  • finding numbers with exactly k distinct digits
  • digit-dynamic-programming questions over numeric ranges
  • analyzing identifiers, PINs, or generated codes

In those settings, the counting step is usually simple. The complexity comes from applying it efficiently across many candidate numbers.

Common Pitfalls

The biggest mistake is forgetting to handle zero correctly. If you loop while n > 0, then 0 needs a special case or the result incorrectly becomes zero.

Another common issue is accidentally counting the minus sign when using string conversion on negative numbers. Applying abs() first avoids that.

It is also easy to confuse "distinct digit count" with "count of digits that appear exactly once." Those are different questions.

Summary

  • Distinct digit count means the number of unique decimal digits present in an integer.
  • A set-based solution is the simplest and most readable approach.
  • A modulo-and-division loop solves the same problem without string conversion.
  • A bitmask is a compact alternative because there are only ten decimal digits.
  • Handle 0 and negative numbers deliberately so the count matches the intended definition.

Course illustration
Course illustration

All Rights Reserved.