Find Maximum in a List Using a Loop

Easy ⏱ 8 min 88% acceptance ★★★★☆ 4.4
Write a function find_max(nums) that returns the largest value in a non-empty list nums, found via a for loop (do not use the built-in max()).

Examples

Example 1
Input
find_max([3, 7, 2, 9, 4])
Output
9
Explanation

9 is the largest value.

Example 2
Input
find_max([-5, -1, -10])
Output
-1
Explanation

Largest of the negative numbers.

Constraints

  • nums is a non-empty list of numbers.
  • Must not use the built-in max() function.

Topics

Loopsfor loop

Companies

MetaNetflix

Hints

Hint 1

Initialize your running maximum to the first element.

Hint 2

Compare each subsequent element and update if it is larger.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.