Fastest algorithm to check if a number is pandigital?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether a number is pandigital looks simple, but performance depends on the exact definition and input size. In most coding tasks, pandigital means digits one through nine each appear exactly once, with no zero. This article compares practical approaches and focuses on fast, branch-light checks.
Define the Variant First
Before optimizing, pin down which variant you need:
- One through nine pandigital means exactly nine digits, each of
1to9once. - Zero through nine pandigital means exactly ten digits, each of
0to9once. - Some tasks allow arbitrary digit sets and custom lengths.
The fastest algorithm for one variant may be wrong for another. For example, a mask target for one through nine differs from zero through nine.
Bitmask Method
A bitmask is usually the fastest general solution in high-level languages and native code. Each digit sets one bit. If a digit repeats, you detect it immediately. At the end, compare against expected mask.
Why it is fast:
- Constant memory.
- One pass over digits.
- Early exit on invalid digit or repetition.
- No sorting or heap allocations.
String and Set Baseline
A readable baseline uses strings and sets. It is concise but often slower due to allocation and hashing overhead.
This can be acceptable for scripts, but for tight loops bitmask usually wins.
Micro-Optimizations for Hot Paths
If this check runs millions of times, you can improve further:
- Reject by digit count first. For one through nine, valid range is from
123456789to987654321. - Use integer arithmetic only, avoiding conversion to string.
- Keep branch order aligned with likely failures for early exits.
In compiled languages, this pattern maps well to low instruction counts and predictable memory behavior.
Zero Through Nine Variant
For zero through nine, adjust the target and expected digit count.
Note that leading zero is impossible in integer representation, which is usually what you want.
Complexity
For both bitmask variants:
- Time complexity is linear in digit count.
- Space complexity is constant.
Since digit count is bounded by machine integer width, this is effectively very small constant-time work in many practical tasks.
Common Pitfalls
- Forgetting to define which digits are required. Fix by documenting variant and tests before coding.
- Accepting repeated digits because only final mask is checked. Fix by duplicate detection during scan.
- Treating zero incorrectly in one-through-nine checks. Fix by immediate reject when digit zero appears.
- Ignoring length constraint. Fix by validating digit count or numeric range.
- Converting to string in performance-critical loops. Fix by using arithmetic extraction with modulo and integer division.
Summary
- Bitmask digit scanning is the most practical fast method for pandigital checks.
- Always define the pandigital variant first.
- Detect duplicates during scanning for early exits.
- Validate length or numeric range to avoid false positives.
- String and set approaches are readable but usually slower for heavy workloads.

