Software Design & Development · Implementation

SDD14 — Min, Max, Count Occurrences

📅 Tue 15 Sep 2026 · P3 (single)
~60 minutes
Learning intentions
Success criteria
Warm up — recap from SDD13
Answer all three questions, then check your answers.
Question 1
What value does linear search typically return to signal that a target was not found in the array?
Question 2
Using stockLevels = [30, 45, 20, 15, 60, 18], what index does a linear search for 15 return?
Question 3
What is the consequence of forgetting to increment the index in linear search's "no match" branch?

Key vocabulary

Minimum
The smallest value present in an array.
Maximum
The largest value present in an array.
Count occurrences
A standard algorithm that counts how many times a specific target value appears in an array.
Running value
A variable that holds the "best so far" result (smallest, largest, or a running total) as an array is traversed.
Traversal
Visiting every element of an array in order, typically with a for loop, to process each one.

Finding the minimum, maximum, and counting occurrences

Three algorithms, one shared shape

SDD13's linear search stopped scanning the moment it found a match. The three standard algorithms in this lesson — finding the minimum, finding the maximum, and counting occurrences — all work differently: each one must examine every single element of the array before it can give a final answer, since there is no way to know the smallest, largest, or total count of matches without having looked at everything. This means all three are naturally written with a for loop that always runs to completion, rather than linear search's early-exit while loop.

Finding the minimum

To find the minimum, a variable (commonly called smallest) is first set to the array's very first element. The algorithm then compares every remaining element to smallest: whenever an element is found that is less than the current smallest, smallest is updated to that new, lower value. By the time every element has been checked, smallest holds the true minimum of the whole array.

Finding the maximum

Finding the maximum works identically, but in reverse: a variable (commonly largest) starts at the first element, and is updated whenever a greater element is found. The only difference between the minimum and maximum algorithms is the comparison operator used (< versus >) — the overall structure, and the reason it works, is exactly the same.

Counting occurrences

Counting occurrences answers a different question: not "what is the smallest/largest value", but "how many times does this specific target value appear?" A counter variable starts at 0, and every element of the array is compared to the target; each time a match is found, the counter is increased by 1. Unlike minimum/maximum, which track a value seen so far, counting occurrences tracks a running total of matches — but the underlying shape (a for loop that visits every element exactly once) is the same.

Why starting at the first element matters

Both the minimum and maximum algorithms start their running value at array[0], then loop through the remaining elements (from index 1 onwards) comparing against it. Starting that loop from index 0 after this initialisation is redundant but not incorrect. The unsafe choice is an arbitrary starting value such as 0: smallest = 0 gives the wrong result when the true minimum is positive, because no element is less than 0, although it may accidentally work when negative values are present. Symmetrically, largest = 0 gives the wrong result when the true maximum is negative, although it may accidentally work when positive values are present. Initialising from array[0] is data-independent and guarantees the result is genuinely present in the array.

Minimum
Tracks the smallest value seen so far, updating whenever a smaller one is found.if array[position] < smallest: smallest = array[position]
Maximum
Tracks the largest value seen so far, updating whenever a bigger one is found.if array[position] > largest: largest = array[position]
Count occurrences
Tracks a running total of matches, incrementing every time the target is seen.if array[position] == target: count = count + 1

Worked examples

Example 1 — Finding the minimum stock level
1
Using the tuck shop's stockLevels array from SDD13:
stockLevels = [30, 45, 20, 15, 60, 18]

def findMinimum(array):
    smallest = array[0]
    for position in range(1, len(array)):
        if array[position] < smallest:
            smallest = array[position]
    return smallest

result = findMinimum(stockLevels)
2
smallest starts at 30 (index 0). Comparing each remaining element: 45 (not smaller), 20 (smaller — smallest becomes 20), 15 (smaller — smallest becomes 15), 60 (not smaller), 18 (not smaller than 15).
result is 15 — confirmed by an actual Python run. Every one of the 6 elements was checked, even though the true minimum was found partway through.
Example 2 — Finding the maximum stock level
1
def findMaximum(array):
    largest = array[0]
    for position in range(1, len(array)):
        if array[position] > largest:
            largest = array[position]
    return largest

result = findMaximum(stockLevels)
2
Only the comparison operator changes from Example 1 — > instead of < — and largest replaces smallest as the running value.
result is 60 — confirmed by an actual Python run.
Example 3 — Counting occurrences of a repeated value
1
A record of tuck shop customers over 6 days: dailyCustomers = [42, 38, 42, 55, 42, 30]. Counting how many days had exactly 42 customers:
def countOccurrences(array, target):
    count = 0
    for position in range(len(array)):
        if array[position] == target:
            count = count + 1
    return count

result = countOccurrences(dailyCustomers, 42)
2
Every element is compared to 42: index 0 (match, count → 1), index 1 (no match), index 2 (match, count → 2), index 3 (no match), index 4 (match, count → 3), index 5 (no match).
result is 3 — confirmed by an actual Python run. Note the loop runs across the whole array (range(len(array)), starting at index 0), unlike minimum/maximum which start comparing from index 1.
Example 4 — Trace table for finding the maximum
1
Tracing findMaximum(dailyCustomers) on [42, 38, 42, 55, 42, 30], recording position, array[position], and largest after each comparison:
positionarray[position]largest (after)
0 (start)4242
13842
24242
35555
44255
53055
Confirmed by an actual Python run: the final result is 55, updated only once (at index 3), even though the array is checked in full.
Now you try
Using dailyCustomers = [42, 38, 42, 55, 42, 30], what does countOccurrences(dailyCustomers, 38) return? What does countOccurrences(dailyCustomers, 99) return?
⚠️ Common mistakes — examiner feedback
📝 Exam tip

When writing or explaining minimum/maximum code, always state that the running value is initialised from the array's own first element, not an arbitrary constant like 0 — examiners specifically check for this. When comparing these three algorithms to linear search, be ready to explain why they must check every element (the answer isn't known until the whole array has been seen) while linear search does not (it only needs to confirm one match exists).

Task Set A — Core questions

Task Set A — Core questions
Work through all questions, then check your answers.
Question 1
Using stockLevels = [30, 45, 20, 15, 60, 18], what is the minimum value?
Question 2
Using the same stockLevels array, what is the maximum value?
Question 3
Using dailyCustomers = [42, 38, 42, 55, 42, 30], how many times does 42 occur?
Question 4
What value should the smallest variable be initialised to before a minimum-finding loop begins?
Question 5
Using dailyCustomers from Question 3, how many times does 99 occur?
Question 6
Why do the minimum, maximum, and count-occurrences algorithms all use a for loop that runs to completion, rather than an early-exit loop like linear search?
Question 7
Explain how the minimum and maximum algorithms are structurally identical, and identify the single difference between them. (3 marks)
Question 8
Write a Python function called countOccurrences that takes an array and a target value as parameters, and returns how many times the target appears in the array.
def countOccurrences(array, target):
    count = 0
    for position in range(len(array)):
        if array[position] == target:
            count = count + 1
    return count
Question 9
A pupil's findMinimum initialises smallest to 0, and the array is stockLevels = [30, 45, 20, 15, 60, 18]. What does their code incorrectly return?
Question 10
Explain why a result of 0 from countOccurrences is a valid, meaningful answer, whereas linear search needs a special sentinel value (-1) to represent "not found". (3 marks)

Task Set B — Extension

Task Set B — Extension · Beyond the specification
Longer written answers — no auto-check. Discuss your answers with your teacher.
Extension 1
Suggest how the minimum and maximum algorithms could be combined into a single function that finds both values in one pass through the array, rather than running two separate loops.
Extension 2
Research Python's built-in min(), max(), and list.count() functions. Suggest why this course teaches the manual, hand-written versions of these algorithms rather than just using the built-in functions.
Extension 3
SDD15 will re-run these algorithms against arrays of records instead of plain arrays. Suggest how finding the "minimum" would need to change if searching for the tuck shop item with the lowest stock level, rather than just the lowest stock number.
📁 File this in OneNote under:
Higher Computing Science → Software Design & Development → SDD14
📌 Teacher notes — not for pupils

Double period. Three genuinely distinct standard algorithms are covered explicitly and separately here (per the original build-status audit's note that this must not collapse into "one running example with variations") — minimum, maximum, and count-occurrences each get their own worked example and their own dedicated Task Set A question.

Suggested timing: 5 min warm-up + vocab · 25 min notes + algo-grid comparison card · 35 min examples 1–4 including the trace table · 10 min "now you try" · 40 min Task Set A · Task Set B as homework/extension.

Key misconception: pupils very reliably initialise smallest/largest to 0 out of habit (perhaps from other counter variables always starting at 0) — Task Set A Q9 deliberately tests this exact bug against stockLevels (an all-positive array), where it produces a plausible-looking but definitely wrong answer of 0. Note this bug's direction is data-dependent: against an all-negative array, the same incorrect initialisation happens to still produce the correct answer, since every element is already less than 0. Worth demonstrating both cases live if time allows, to show the bug isn't reliably self-revealing.

Deliberately reuses SDD13's stockLevels array for minimum/maximum continuity, but introduces a fresh dailyCustomers array with a genuinely repeated value for the count-occurrences examples, since stockLevels/tuck shop prices have no natural repeats to count.

SQA command words covered: explain, describe, write code, trace/complete a trace table.