Given an array nums
of integers, return how many of them contain an even number of digits.
nums
?
0
since no numbers have an even number of digits.nums.length
) will be in the range [1, 500].nums
is in the range [1, 10^5].To determine if a number has an even number of digits:
To count the total:
nums
.nums
.def findNumbers(nums):
even_digits_count = 0
for num in nums:
# Convert the number to string and check its length
if len(str(num)) % 2 == 0:
even_digits_count += 1
return even_digits_count
# Example test case
nums = [12, 345, 2, 6, 7896]
print(findNumbers(nums)) # Output should be 2
This code will correctly count and return the number of integers in the nums
array that have an even number of digits.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?