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
- best = nums[0]
- More numbers?
- x = next number
- x > best?
- best = x
- Return best