Start learning free

LESSON 266 OF 678 · TUPLES, SORTING & BUILT-INS

Min and Max in Swift: Both in One Loop, Returned as a Tuple

Practice Read the full explanation

You get an array of integers nums. Return its smallest and its largest number together, as a pair.

The smallest number comes first. The function's return type shows how your language holds the pair.

Find both in one loop. Don't use a built-in min or max function.

 

Example 1:

Input: nums = [4, -2, 9, 0]
Output: (-2, 9)
Explanation: The smallest number is -2.
The largest number is 9.

Example 2:

Input: nums = [7]
Output: (7, 7)
Explanation: The only number is both the smallest and the largest.

Example 3:

Input: nums = [-5, -1, -8]
Output: (-8, -1)
Explanation: All the numbers are below 0.
The smallest is -8, and the largest is -1.

 

Constraints:

  • nums has at least one number.
  • Every number is between -106 and 106.
STEP 1 OF 5
    Ctrl + Enter to check

    SWIFT · TUPLES, SORTING & BUILT-INS

    Min And Max in Swift, explained

    Problem overview

    The function minAndMax receives an array of integers, nums, which always has at least one number. It returns the smallest and the largest number together, as a tuple (low: Int, high: Int) with the smallest first. The statement asks for a single loop and no built-in min or max.

    For [4, -2, 9, 0] the result is (low: -2, high: 9). For [7] it is (low: 7, high: 7), because one number is both the smallest and the largest.

    Approach

    Keep two variables, low and high, the smallest and the largest number seen so far. Look at each number once. If it is smaller than low, it becomes the new low. If it is larger than high, it becomes the new high. When the loop ends, both variables hold the answer.

    The starting values matter. Starting both at 0 is wrong when every number is above 0 (the true smallest would never replace 0) or every number is below 0. The first number is always a real candidate, so both variables start at nums[0]. That is safe because the array is never empty.

    Here is the loop on [4, -2, 9, 0]. Both variables start at 4. The table shows low and high after each number is handled.

    numlowhigh
    444
    -2-24
    9-29
    0-29

    A single pass is enough because each number only has to be compared with the two values already kept. Nothing needs to be sorted or remembered beyond those two.

    Step by step

    1. Declare low and high with var, both set to nums[0]. They change inside the loop, so let won't do.
    2. Loop over nums with for num in nums. One pass checks each number against both variables.
    3. Update low when num < low.
    4. Update high when num > high. This is a second if, not an else: the two tests are independent, and each number is checked against both.
    5. Return (low, high) after the loop.

    The solution in Swift

    class Solution {
        func minAndMax(_ nums: [Int]) -> (low: Int, high: Int) {
            var low = nums[0]
            var high = nums[0]
            for num in nums {
                if num < low {
                    low = num
                }
                if num > high {
                    high = num
                }
            }
            return (low, high)
        }
    }
    

    Two Swift details:

    • The return type supplies the labels. return (low, high) has no labels of its own, but the declared type (low: Int, high: Int) gives them, so the caller can write result.low and result.high. For [4, -2, 9, 0], result.low is -2 and result.high is 9.
    • nums[0] traps on an empty array. Swift stops the program with an index-out-of-range error instead of returning garbage. The constraints rule out an empty array, so no check is needed here.

    Edge cases and common mistakes

    • One number. [7] gives (low: 7, high: 7).
    • All negative numbers. [-5, -1, -8] gives (low: -8, high: -1). A high that started at 0 would wrongly stay 0.
    • All positive numbers. [3, 8, 5] gives (low: 3, high: 8). A low that started at 0 would wrongly stay 0.
    • Swapped return values. The smallest comes first, so (high, low) returns the pair in the wrong order for any array with two different values.
    • Putting both updates in one branch. If the assignment to high sits inside the if num < low block, high is updated only for numbers that are new minimums, and [3, 8, 5] returns (low: 3, high: 3).
    • Built-ins. Swift arrays have min() and max() methods, which this lesson leaves out so that you practise the loop they hide.

    Testing

    • [4, -2, 9, 0] gives (low: -2, high: 9).
    • [7] gives (low: 7, high: 7).
    • [-5, -1, -8] gives (low: -8, high: -1).
    • [3, 8, 5] gives (low: 3, high: 8).
    Keep going with Swift500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free