DashboardArray maximum
Back to Forge
Easy CHALLENGE

Array Maximum

Find the largest element in an array of numbers.

Logical Approach_

1
Assume the first element as current max.
2
Iterate through the array starting from the second element.
3
If current element is greater than current max, update current max.
4
Return the final max value.

Variations Hub_

Find the second largest element.
Find both min and max in a single pass.
Find max element in a 2D array.
Implementation Protocol (JS)
function findMax(arr) {
  if (arr.length === 0) return null;
  let max = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}

// Modern ES6 approach
const getMax = (arr) => Math.max(...arr);

Line-by-Line Breakdown_

Point 1

Line 3: Initialize max with the first element of the array.

Point 2

Line 4-8: Standard loop through remaining elements with comparison.

Point 3

Line 13: Using spread operator (...) with Math.max for a concise solution.

Ready to run this code?

Execute in the interactive playground for deep analysis.