Find Numbers with Even Number of Digits

Back to index

From the LeetCode problem with the same name. The problem statement is:

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

In order to figure out how many digits an integer has (when expressed in base 10) we can use the logarithm function (with base 10).

class Solution {
  public int findNumbers(int[] nums) {
    int n = 0;
    for (int i = 0; i < nums.length; i++) {
      if (Math.floor(Math.log10(nums[i])) % 2 == 1) n++;
    }
    return n;
  }
}