Implement the method findMax
that takes an array of integers and returns the largest (maximum) value in the array.
Learner Level
Loop Array Linear Search
Write a function that receives an array of integers and returns the largest number present in the array. The function should compare all elements and return the maximum value found.
To find the maximum number in an array:
-
Initialize a Max Variable
Set a variable to hold the current maximum value (e.g., the first element of the array). -
Iterate Through the Array
Loop over the array starting from the second element. -
Compare and Update
If the current element is greater than the stored maximum, update the maximum. -
Return the Result
After the loop ends, return the maximum value found.
Pseudocode:
Function findMax(nums, len):
max ← nums[0]
For i from 1 to len - 1:
If nums[i] > max:
max ← nums[i]
Return max