You are given an array of integers. Write a function find_first_max_index
that takes this array as an argument and returns the index of the first occurrence of the maximum value in the array. If the maximum value occurs more than once, the function should return the index of the first occurrence.
The function takes an array of integers as input. You can assume that the array is non-empty and contains at least one integer.
The function should return an integer representing the index of the first occurrence of the maximum value in the array.
assert find_first_max_index([1, 3, 2, 3, 1]) == 1
assert find_first_max_index([4, 4, 4, 4, 4]) == 0
assert find_first_max_index([1, 2, 3, 4, 5]) == 4
In the first example, the maximum value is 3 and it first occurs at index 1. In the second example, the maximum value is 4 and it first occurs at index 0. In the third example, the maximum value is 5 and it first occurs at index 4.
Your current solution is already quite good, but there are a few potential improvements you could make:
def find_first_max_index(arr: List[int]) -> int:
if not arr:
return -1
max_value = arr[0]
max_index = 0
for i in range(1, len(arr)):
if arr[i] > max_value:
max_value = arr[i]
max_index = i
return max_index