Learner Level

Implement the method findMax that takes an array of integers and returns the largest (maximum) value in the array.

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.

Example 1

Input
nums (int[]) = [1, 4, 2, 9, 7]
len (int) = 5
Returns (int) 9

Example 2

Input
nums (int[]) = [-10, -5, -3]
len (int) = 3
Returns (int) -3

Example 3

Input
nums (int[]) = [0, 0, 0, 0]
len (int) = 4
Returns (int) 0

Example 4

Input
nums (int[]) = [5]
len (int) = 1
Returns (int) 5

To find the maximum number in an array:

  1. Initialize a Max Variable
    Set a variable to hold the current maximum value (e.g., the first element of the array).

  2. Iterate Through the Array
    Loop over the array starting from the second element.

  3. Compare and Update
    If the current element is greater than the stored maximum, update the maximum.

  4. 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
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.