All topics
7

Find the biggest number

Free

1. The problem

You have a row of numbers, say the scores of a game: [4, 9, 2, 7]. Which one is the biggest? You see the answer at once. A computer looks into only one box at a time and can't take in the whole row at a glance. How does it find the biggest number?

2. The idea: keep the best so far

Here is a trick you will use many more times. Walk through the list once and keep the best so far in a box: the biggest number among the ones you have already seen.

Start by treating the first number as the biggest. Then look at each next number. If it is bigger than the number in the box, put the new number in the box. When the list ends, the box holds the answer.

4 9 2 7 best so far = 9

Trace on [4, 9, 2, 7]: the box starts with 4 → see 9, it is bigger, the box now holds 9 → see 2, smaller than 9, nothing changes → see 7, also smaller than 9 → answer 9.

4. Where this shows up

"Highest score," "most recent order," "closest driver," "cheapest option": all of these are the same single pass in which you keep the best so far. Swap "bigger" for "smaller" and you find the smallest. Swap in any other comparison rule and you find the best by that rule. You will meet this small trick everywhere.

On LeetCode

Problems that ask for the largest, smallest or best value in one pass over an array. Keep the best so far in a variable and compare each new item with it.

Write it yourself

In the editor, largest(nums) waits with a // TODO mark. Make a variable for the best so far and put the first number of the list in it. Loop over the remaining numbers and replace the best each time you find a bigger number. At the end, return the best.

Start with the first number, and not with 0. The tests include the list [-4, -9, -2]. If you start at 0, no number in it is bigger than 0, and the function returns 0, a number that is not in the list at all. The right answer is -2.

The tests also include a list with one number (the answer is that number) and an empty list. An empty list has no biggest number, so return null (None in Python). The tests call the empty list the "negative case". You have to decide in advance what happens when there is nothing to compare, so the program doesn't crash.

Practice

Find the biggest number

largest(nums) -> the biggest number, or null if nums is empty

// Step 07: find the biggest number. You write the whole function.
//
// largest(nums)
//   nums: number[]
//   returns the biggest number, or null if nums is empty.

export function largest(nums) {
  // TODO: keep the best-so-far while walking nums; return it (null if empty)
}

Running as a guest

The tests run right here, no account needed. Your code is kept in this browser only. A free account keeps it on every device, marks the step done and shows where you are in the course.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy