Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Find Maximum

Working With Arrayseasy

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Implement `findMax(arr)` returning the largest number in a non-empty array.

Sample tests

Input: findMax([3,7,2,8,5])
Output: 8
Input: findMax([-1,-5])
Output: -1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Math.max() with no arguments returns -Infinity; only safe because the array is non-empty.

Learning resources

  • MDN: Array
Visualize this concept: Sorting Arrays →
Approach & explanation (try first)

Spreading the array into Math.max returns the largest element.

Loading...
⌘/Ctrl + Enter

Run your code to see results.