Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Index of Maximum

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 156: Data Transformations: map, filter, reduce

Implement `indexOfMax(nums)` returning the index of the first occurrence of the largest value in a non-empty array.

Sample tests

Input: indexOfMax([3,7,7,2])
Output: 1
Input: indexOfMax([5])
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Using >= updates to the last max instead of the first.

Learning resources

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

Scanning and updating only when strictly greater returns the index of the first maximum.

Loading...
⌘/Ctrl + Enter

Run your code to see results.