Find Largest Numbers in Arrays

Return an array consisting of the largest number from each provided sub-array.

const largestNumbers = arr => {
  return arr.map(subarr => {
    return subarr.reduce((largest, curr) => {
      return Math.max(largest, curr)
    }, subarr[0]);
  });
}

largestNumbers([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); // [5, 27, 39, 1001]

One liner solution

const largestNumbers = arr => 
  arr.map(subarr => subarr.reduce((largest, curr) => Math.max(largest, curr), subarr[0]));

Here’s a slight modification of the above algorithm, which returns the largest number instead.

const largestNumber = arr => arr.reduce((largest, subarr) => {
    largest = Math.max(...subarr);
    return largest;
  }, arr);
largestNumber([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); // 10001