findMax

Easy · Array Manipulations · 2 test cases

Given an array of numbers, return the largest number.

Examples

findMax([1, 5, 3, 9, 2]) → 9
findMax([-1, -5, -3]) → -1

Starter code

function findMax(arr) {

}

Complexity of the optimal solution

Time O(n), space O(n). The spread syntax `...arr` creates a copy of the array elements for the function call. `Math.max` must then iterate through all n elements to find the maximum. The space is O(n) due to the arguments passed by the spread operator.

Solve findMax in the browser