Find Numbers with Even Number of Digits: Digit Counting
Given an array nums of integers, return how many of them contain an even number of digits. For example, given [12, 345, 2, 6, 7896], the answer is 2 because 12 has 2 digits and 7896 has 4 digits.
This is LeetCode 1295: Find Numbers with Even Number of Digits, an easy problem that tests your ability to count digits and filter an array based on a simple condition.
Why this problem matters
This problem is one of the simplest array problems on LeetCode, but it introduces two skills that show up in harder problems constantly. First, you need to extract a property from each element (in this case, the digit count). Second, you need to filter and count based on that property. These two operations form the backbone of problems like counting elements with specific characteristics, grouping by properties, and partition-based logic.
It also gives you a chance to think about different ways to count digits. You can convert to a string, use logarithms, or divide in a loop. Each approach has tradeoffs, and understanding them prepares you for problems where efficiency or constraints force a particular choice.
The key insight
Every number can be decomposed into its individual digits. You just need to count how many digits each number has and check whether that count is even. The simplest way in Python is to convert the number to a string and check the length. For each number, len(str(num)) gives the digit count, and len(str(num)) % 2 == 0 tells you whether it qualifies.
The solution
def findNumbers(nums):
count = 0
for num in nums:
if len(str(num)) % 2 == 0:
count += 1
return count
You iterate through the array once. For each number, convert it to a string, check if the length is even, and increment a counter if it is. That is the entire algorithm.
You can also write this as a one-liner using sum with a generator expression:
def findNumbers(nums):
return sum(1 for num in nums if len(str(num)) % 2 == 0)
Both versions do the same thing. The explicit loop is easier to read and explain in an interview. The one-liner is more Pythonic.
Converting to a string is perfectly fine for this problem since the constraints say each number is between 1 and 100,000. For very large numbers or performance-critical code, you could use math.log10 or repeated division instead, but string conversion is clean and readable here.
Visual walkthrough
Step 1: Check 12
12 has 2 digits. 2 is even, so increment count to 1.
Step 2: Check 345
345 has 3 digits. 3 is odd, so count stays at 1.
Step 3: Check 2
2 has 1 digit. 1 is odd, so count stays at 1.
Step 4: Check 6
6 has 1 digit. 1 is odd, so count stays at 1.
Step 5: Check 7896
7896 has 4 digits. 4 is even, so increment count to 2. Final answer: 2.
Each step breaks the number into its individual digits, counts them, and checks if the count is even. After processing all five numbers, we found two with an even digit count: 12 (2 digits) and 7896 (4 digits).
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Digit counting | O(n * d) | O(1) |
Here, n is the number of elements in the array and d is the average number of digits per number. For each element, counting digits takes O(d) time (whether you use string conversion, log, or division). Since d is at most 6 for the given constraints (numbers up to 100,000), this is effectively O(n). The space is O(1) because you only maintain a single counter variable. The string conversion creates a temporary string, but it does not scale with the input size.
The building blocks
1. Digit extraction
The act of breaking a number into its individual digits or determining properties about them (count, sum, specific digit values). This micro-pattern appears in many problems:
digit_count = len(str(num))
You can also count digits without string conversion using integer arithmetic:
def count_digits(num):
if num == 0:
return 1
count = 0
while num > 0:
count += 1
num //= 10
return count
The division approach divides by 10 repeatedly, incrementing a counter each time, until the number reaches 0. Both methods give the same result.
2. Count-if pattern
The pattern of iterating through a collection and counting elements that satisfy a condition:
count = 0
for item in collection:
if condition(item):
count += 1
This is one of the most fundamental array patterns. You will use it in problems that ask "how many elements satisfy X?" whether X is a parity check, a range check, a digit property, or any other predicate. In Python, sum(1 for x in arr if condition(x)) is the idiomatic shorthand.
Edge cases
- All single-digit numbers like
[1, 5, 9]: every number has 1 digit (odd), so the answer is 0. - All numbers have even digit counts like
[10, 20, 1000]: every number qualifies, so the answer equals the array length. - Numbers at the boundary like
[10, 99, 100, 999]: 10 and 99 have 2 digits (even), while 100 and 999 have 3 digits (odd). Make sure your digit counting handles the transition between digit lengths correctly. - The number 1 has exactly 1 digit (odd). The number 10 has exactly 2 digits (even). The boundary between 1-digit and 2-digit numbers is at 10.
- Maximum constraint value of 100,000 has 6 digits (even), so it would be counted.
From understanding to recall
You have read the digit counting approach and it makes sense. Convert each number to a string, check the length, count the ones with even length. Three lines of code.
But the real value is not in solving this specific problem. It is in internalizing the building blocks: digit extraction and the count-if pattern. These two pieces show up in dozens of other problems. When you see a problem that asks you to group numbers by digit sum, or count elements matching a condition in a sliding window, or filter candidates in a search, the same patterns apply.
Spaced repetition helps you lock these patterns in. You practice writing digit extraction and count-if from scratch at increasing intervals until the code flows automatically. That way, when these building blocks appear inside a harder problem, you do not waste time reinventing them.
Related posts
- Contains Duplicate - Another easy array problem that introduces the "iterate and check" pattern using a set
- Find All Numbers Disappeared in an Array - Array element property analysis with a counting twist
- Single Number - Filtering an array to find elements with a special property, using XOR instead of counting
Recognizing that this problem is just "count digits, check parity" takes about two seconds once you have drilled the building blocks. But getting to that automatic recognition requires practice. CodeBricks breaks problems like this into their reusable pieces and drills them with spaced repetition so the patterns become second nature.