Skip to content
IGCSE·Tuition
Computer Science · Lesson

Find the largest value in a list

Finding the biggest number is easy to do by eye and surprisingly easy to get wrong when you have to write the steps.

On this page
  1. What are the steps?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

To find the maximum of a list, keep one variable holding the largest value seen so far and compare every other item with it. Each time an item is larger, it replaces the stored value.

This lesson uses the loops from tracing count-controlled loops on an array. It sits inside repetition and arrays, and the same pattern gives minimum, count and total.

What are the steps?

  1. Set Max to the first item in the list.
  2. Loop through the remaining items, from position 2 to the last position.
  3. If the current item is greater than Max, copy it into Max.
  4. After the loop, Max holds the largest value.

Worked example

An array Scores[1:6] holds 35, 48, 22, 48, 51, 40.

Max ← Scores[1]
FOR i ← 2 TO 6
   IF Scores[i] > Max
      THEN
         Max ← Scores[i]
   ENDIF
NEXT i
OUTPUT Max

Max starts as 35.

iScores[i]Scores[i] > Max?Max after
24848 > 35 true48
32222 > 48 false48
44848 > 48 false48
55151 > 48 true51
64040 > 51 false51

The output is 51. Position 4 holds an equal value, 48, and does not replace Max because 48 > 48 is false.

To also report where the maximum is, store the position as well:

Max ← Scores[1]
MaxPos ← 1
FOR i ← 2 TO 6
   IF Scores[i] > Max
      THEN
         Max ← Scores[i]
         MaxPos ← i
   ENDIF
NEXT i
OUTPUT MaxPos, Max

For the same data, MaxPos changes at i = 2 and i = 5. The output is 5, 51.

The mistake to watch for

A common slip is to start with Max ← 0.

Mistaken algorithm: Max ← 0, then compare every item including the first.

With the temperatures -5, -2 and -9, no value is greater than 0, so the algorithm outputs 0. That value is not in the list.

The correction is Max ← Temps[1], then loop from position 2. Trace it: Max is -5, then -2 > -5 is true so Max becomes -2, then -9 > -2 is false. The output is -2, which is correct.

Check yourself

1. Trace the algorithm on the data 8, 3, 12, 12, 5. What is Max after each item?

Show answer

Start with 8. Then 3 > 8 false (8), 12 > 8 true (12), 12 > 12 false (12), 5 > 12 false (12). The final value is 12.

2. Which single symbol changes to find the minimum of the same data, and what is the result?

Show answer

Change > to <. Start with 8, then 3 < 8 true (3), then 12, 12 and 5 are not less than 3. The minimum is 3.

3. For the data 6, 9, 9, 2 stored in Data[1:4], what position does the position version give, and what would change with >=?

Show answer

With >, 9 at position 2 is stored first, and the second 9 does not replace it, so the position is 2. With >=, the second 9 replaces it, so the position is 3. The value is 9 in both cases.

Where this leads next

Next, look closely at what the loop variable i means compared with the item it points to in using an array index without confusing it with a value. To try your own list, use the Python reasoning sandbox and compare its result with your trace table.

A teacher in online one-to-one Computer Science tuition can give you awkward lists, such as negatives or repeated values, so you learn to predict where an algorithm fails.

Questions people ask

Why start Max with the first item instead of 0?

Starting at 0 only works when every value is above 0. If all the values are negative, 0 is never beaten and the algorithm outputs a value that is not in the list. Starting with the first item always gives a real value from the list to compare against.

What changes if I want the smallest value?

Change the comparison from > to < and name the variable Min. Everything else stays the same: start with the first item and compare each remaining item with it.

What happens when the largest value appears twice?

With > the first largest value stays, because an equal value does not replace it. With >= the last one wins. The maximum value is the same either way, but a stored position will differ.

Updated:

Your next step

If you can find the maximum by eye but your algorithm fails on awkward data, a teacher can feed it new lists with you until you can predict where it breaks.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students