Find Numbers with Even Number of Digits

Modulus

To find out whether a number is even or not, we use the modulus operator. Y % X implies that Y is divided by X, and whatever the remainder is will be the result.

If we divide a number by 2, and it's odd we will have a remainder.

6 % 2 == 0 # False

The number being divided is called the dividend, and the number that divides the dividend is called the divisor.

We can also use modulus for other things, such as checking if a number is odd. Or turning a list into a loop. If we have a list that is size 6, and we want to iterate 9 numbers but have it loop back around, we can use modulus for this.

6 % 9 = 3, which means our index will be at 3.

Given an array nums of integers, return how many of them contain an even number of digits.

Input: nums = [12,345,2,6,7896]
Output: 2
Explanation: 
12 contains 2 digits (even number of digits). 
345 contains 3 digits (odd number of digits). 
2 contains 1 digit (odd number of digits). 
6 contains 1 digit (odd number of digits). 
7896 contains 4 digits (even number of digits). 
Therefore only 12 and 7896 contain an even number of digits.

Last updated