Start learning free

LESSON 265 OF 675 · TUPLES, SORTING & BUILT-INS

Min and Max in One Pass in TypeScript: Returning 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

    TYPESCRIPT · TUPLES, SORTING & BUILT-INS

    Min And Max in TypeScript, explained

    Problem overview

    The method minAndMax gets an array of integers, nums, and returns its smallest and largest number together as a tuple [number, number], smallest first. The array has at least one number. The task asks for one loop and no built-in min or max.

    For [4, -2, 9, 0] the result is [-2, 9]. For [7] it is [7, 7], since the only number is both the smallest and the largest. For [-5, -1, -8] it is [-8, -1].

    Approach

    Keep two variables, low and high, that always hold the smallest and largest numbers seen so far. Look at each number once: if it is below low, it becomes the new low; if it is above high, it becomes the new high. After the last number, both variables are final.

    Where to start

    Both variables start at nums[0]. Starting at 0 would be wrong when every number is above 0 (the low would stay 0) or every number is below 0 (the high would stay 0). The first number is always a real candidate. This also covers the one-number array without extra code.

    Step by step

    1. Declare low and high as numbers with let, both set to nums[0]. They need let because they change.
    2. Loop over nums with for...of.
    3. Inside the loop, compare each num with low and update it when num is smaller.
    4. Below that, still inside the loop, compare num with high and update it when num is larger. These are two separate if statements, not an else: every number is tested against both bounds,, and a single number can update at most one of them.
    5. After the loop, return [low, high].

    The solution in TypeScript

    class Solution {
        minAndMax(nums: number[]): [number, number] {
            let low: number = nums[0];
            let high: number = nums[0];
            for (let num of nums) {
                if (num < low) {
                    low = num;
                }
                if (num > high) {
                    high = num;
                }
            }
            return [low, high];
        }
    }
    

    The return type [number, number] is a tuple type: the compiler checks that the returned array has two numbers. The tuple is an ordinary array at run time, so a caller can print it or destructure it with const [smallest, largest] = ....

    The first pass looks at nums[0] against itself, which changes nothing. That is harmless.

    Why one pass is enough

    After each number, low is the smallest of the numbers looked at so far and high is the largest. That stays true when the next number arrives, because the new smallest is either the old low or the new number. When the loop ends, "so far" means the whole array. Finding both values in one loop does half the work of two separate loops, though both are linear: the time grows in step with the array's length, and only two extra variables are used.

    Testing

    These results come from calling the method on the statement's examples:

    numsResult
    [4, -2, 9, 0][-2, 9]
    [7][7, 7]
    [-5, -1, -8][-8, -1]

    Common mistakes

    • Starting at 0. With [-5, -1, -8], a high that starts at 0 would end as 0, a number that is not in the array.
    • Returning [high, low]. The smallest comes first.
    • Using the built-ins. Math.min(...nums) is the short way, and a later lesson uses it, but this exercise asks for one loop.
    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free