The challenge

Given an array of positive integers, return the number with the most digits. If two numbers have the same number of digits, return the first one in the array.

Examples:

findLongest([1, 10, 100]);     // → 100
findLongest([9000, 8, 800]);   // → 9000
findLongest([8, 900, 500]);    // → 900

This puzzle is asked in JavaScript-flavoured Codewars, HackerRank, and front-end interviews. The trick is the tie-breaker rule — the first occurrence wins.

Solution 1: reduce() with a digit count

The cleanest functional approach in JavaScript:

function findLongest(arr) {
  return arr.reduce((longest, n) =>
    String(n).length > String(longest).length ? n : longest
  );
}

Why this works: reduce walks left-to-right and only swaps when the current value has strictly more digits than the accumulator. Equal-digit numbers never displace the accumulator, so the first occurrence is preserved.

Solution 2: classic for-loop (no built-ins)

Some interviewers explicitly say “do not use built-in higher-order functions”. Here is the equivalent without reduce:

function findLongest(arr) {
  let longest = arr[0];
  let longestDigits = String(arr[0]).length;

  for (let i = 1; i < arr.length; i++) {
    const digits = String(arr[i]).length;
    if (digits > longestDigits) {
      longest = arr[i];
      longestDigits = digits;
    }
  }

  return longest;
}

Why this works: Track the current winner and its digit count. Replace only when the new value is strictly longer. Using > (not >=) preserves the first match on ties.

Solution 3: Math.log10 (no string allocation)

For very long arrays of large integers, allocating a string per element matters. Use logarithms instead:

function findLongest(arr) {
  const digits = (n) => Math.floor(Math.log10(n)) + 1;
  let longest = arr[0];

  for (let i = 1; i < arr.length; i++) {
    if (digits(arr[i]) > digits(longest)) {
      longest = arr[i];
    }
  }

  return longest;
}

Math.log10(100) is 2, and 100 has 3 digits — so floor(log10(n)) + 1 gives the digit count without any string work. Note: this fails for 0 (log10 of zero is -Infinity), but the challenge states all inputs are positive integers.

Complexity

Solution Time Space
reduce() O(n × d) O(1) extra
for-loop O(n × d) O(1) extra
Math.log10 O(n) O(1) extra

Where n is array length and d is digits per number. For typical interview inputs (≤ 10⁹), d ≤ 10, so all three are effectively linear.

Edge cases

  • Single-element array: findLongest([42]) returns 42.
  • All same digit count: findLongest([10, 20, 30]) returns 10 (first wins).
  • Very large numbers (BigInt): String(n).length works for BigInt if you append n.toString().length. Math.log10 does not — fall back to string counting.

Test cases

console.assert(findLongest([1, 10, 100]) === 100);
console.assert(findLongest([9000, 8, 800]) === 9000);
console.assert(findLongest([8, 900, 500]) === 900);
console.assert(findLongest([3, 40000, 100]) === 40000);
console.assert(findLongest([1, 200, 100000]) === 100000);
console.assert(findLongest([42]) === 42);