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:
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:
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:
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:
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
kdistinct 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
0and negative numbers deliberately so the count matches the intended definition.

