Category : Easy

**Problem**

Given an array `nums`

of integers, return how many of them contain an **even number** of digits.

https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

**Examples**

**Example 1:**

1 2 3 4 5 6 7 8 9 | 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. |

**Example 2**:

1 2 3 4 | Input: nums = [555,901,482,1771] Output: 1 Explanation: Only 1771 contains an even number of digits. |

**Constraints**

`1 <= nums.length <= 500`

`1 <= nums[i] <= 10^5`

**Solution Approach**

The problem states that we need to find a count of all the numbers in the array which has even number of digits. There are many ways to find the number of digits in a number. You can use the technique which I have showed in palindrome number which can be accessed here. I am going to introduce a new technique which will be much more efficient if we only want to know the number of digits in a number without wanting to know what those digits are.

We can make use of strings to convert a number into a string so that the length calculation becomes easier using the length() method of Java.

To perform our calculation we need to initialise a count variable to keep the count of all the numbers with even number of digits. We loop through the array next and one by one we convert the integer to string using toString() method and calculate the length. If the length is divisible by 2 then we increase the value in the count variable. At the end of the loop, we just return the count variable’s value as our answer.

**Solution code**

1 2 3 4 5 6 7 8 9 10 | class Solution { public int findNumbers(int[] nums) { int count = 0; for(int i=0; i<nums.length; i++){ if(Integer.toString(nums[i]).length()%2==0) count += 1; } return count; } } |

For more Leetcode explained solutions visit Leetcode Solutions. If you like capture the flag challenges visit here.

Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via Contact page I will surely respond. Happy Leetcoding