Start learning free

LESSON 261 OF 671 · DESTRUCTURING, SORTING & BUILT-INS

Min and Max in One Pass in JavaScript

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

    JAVASCRIPT · DESTRUCTURING, SORTING & BUILT-INS

    Min And Max in JavaScript, explained

    Problem overview

    minAndMax(nums) receives an array of integers with at least one number. It returns the smallest and the largest number together, as an array of two values with the smallest first. For [4, -2, 9, 0] the answer is [-2, 9]. For a one-number array such as [7] the number is both, so the answer is [7, 7]. For [-5, -1, -8] it is [-8, -1].

    The statement asks for one loop and no built-in min or max.

    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.

    Both start at nums[0]. Starting at 0 would be wrong: if every number is above 0, a starting low of 0 would never be replaced, and if every number is below 0, high would stay at 0. The first number is always a real candidate.

    Step by step

    1. Declare low and high with let, both set to nums[0].
    2. Loop over the array with for...of.
    3. Inside the loop, an if lowers low when num is smaller.
    4. A second, separate if raises high when num is larger. It is not an else: with one number, both checks are false and nothing changes, which is correct.
    5. After the loop, return [low, high].

    The solution in JavaScript

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

    JavaScript has no tuple type, so an array of two values is how a function returns a pair. The caller can unpack it with const [lo, hi] = .... Running the three examples gives:

    [-2, 9]
    [7, 7]
    [-8, -1]
    

    Tracing the first example

    For [4, -2, 9, 0], low and high both start at 4. After each number they hold:

    num=4 low=4 high=4
    num=-2 low=-2 high=4
    num=9 low=-2 high=9
    num=0 low=-2 high=9
    

    The loop looks at 4 first, even though it is also the starting value. That extra pass changes nothing, and it keeps the loop simple. The last number, 0, is neither smaller than -2 nor larger than 9, so both variables stay as they are.

    Complexity

    The loop visits each number once and does two comparisons, so the time is O(n). The two variables and the returned pair take a fixed amount of memory, so the extra space is O(1). Finding the smallest and then the largest in two separate loops would also be O(n), but it reads the array twice.

    Edge cases and common mistakes

    • Starting at 0. For [5, 2, 8] the right answer is [2, 8], but a version that starts both variables at 0 returns [0, 8].
    • Using else if for the second check. With nums[0] as the start this happens to work for most inputs, but two independent ifs are simpler to reason about.
    • Returning [high, low]. The smallest comes first. The tests compare the pair in order, so the right numbers in the wrong order still fail.
    • Reaching for Math.min(...nums). It is handy in real code, but this lesson asks for the loop.
    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free