Largest number in a list

Return the largest number in the list nums, without using max() / Math.max().

Write largest(nums) in Python, largest(nums) in JavaScript or TypeScript, or int largest(int nums[], int n) in C. Level: Easy.

Examples

Input: nums = [3, 7, 2, 9, 4]
Output: 9
Input: nums = [-5, -2, -9]
Output: -2
  • 1 ≤ length of nums ≤ 1000
  • Numbers can be negative

Hint

Start with the FIRST number as the best so far, not 0. With [-5, -2, -9], starting at 0 gives the wrong answer.

The approach, step by step

  1. best = nums[0]
  2. More numbers?
  3. x = next number
  4. x > best?
  5. best = x
  6. Return best

← All practice problems

Try AI Flowchart free →