Start learning free

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

Total Without Extremes in TypeScript: Sum Minus Min and Max

Practice Read the full explanation

You get an array of integers nums. Return their sum without the smallest and the largest value.

Some contests score this way. Each judge gives a score. The lowest and the highest score are dropped. So one very strict or very kind judge can't decide alone.

Drop only one smallest value and one largest value. If the smallest value appears twice, the other copy still counts. The same goes for the largest value.

 

Example 1:

Input: nums = [7, 9, 6, 8, 10]
Output: 24
Explanation: The smallest value is 6 and the largest is 10.
The rest add up to 7 + 9 + 8 = 24.

Example 2:

Input: nums = [5, 1, 5, 9, 1]
Output: 11
Explanation: The smallest value, 1, appears twice.
Only one 1 is dropped, and so is the 9.
5 + 5 + 1 = 11.

Example 3:

Input: nums = [-4, 3, -2]
Output: -2
Explanation: -4 is the smallest value and 3 is the largest.
Only -2 is left.

 

Constraints:

  • nums has 3 to 100 numbers.
  • Every number is between -1000 and 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · TUPLES, SORTING & BUILT-INS

    Trimmed Total in TypeScript, explained

    Problem overview

    The method trimmedTotal gets an array of integers, nums, and returns their sum without one smallest value and one largest value. Only one of each is dropped: if the smallest value appears twice, the other copy still counts, and the same goes for the largest. The array has 3 to 100 numbers, each between -1000 and 1000.

    For [7, 9, 6, 8, 10] the answer is 24: the smallest is 6, the largest is 10, and 7 + 9 + 8 = 24. For [5, 1, 5, 9, 1] it is 11: one 1 and the 9 are dropped, leaving 5 + 5 + 1. For [-4, 3, -2] it is -2.

    Approach

    You could sort a copy and add up everything but the first and last item. There is a simpler way: add up everything, then subtract the smallest value once and the largest value once. Subtracting each exactly once removes exactly one smallest and one largest value, even when they repeat. That is the rule in the statement.

    The three numbers come from the built-ins: reduce for the total, Math.min(...nums) for the smallest and Math.max(...nums) for the largest. The dots spread the array out into separate arguments, because Math.min and Math.max take numbers, not an array.

    Step by step

    1. Store the sum in total with nums.reduce((sum, x) => sum + x, 0). The 0 is the start value of the sum.
    2. Store Math.min(...nums) in smallest. You get the value once, even if it appears twice.
    3. Store Math.max(...nums) in largest.
    4. Return total - smallest - largest.

    The solution in TypeScript

    class Solution {
        trimmedTotal(nums: number[]): number {
            let total: number = nums.reduce((sum, x) => sum + x, 0);
            let smallest: number = Math.min(...nums);
            let largest: number = Math.max(...nums);
            return total - smallest - largest;
        }
    }
    

    The second example shows the duplicates rule. The total of [5, 1, 5, 9, 1] is 21, the smallest is 1 and the largest is 9, so the result is 21 - 1 - 9 = 11. The second 1 stays in the sum.

    The third example has negatives: the total is -3, the smallest is -4 and the largest is 3, so -3 - (-4) - 3 = -2. Subtracting a negative number adds it back, which is what dropping it means.

    Why this is simple and fast

    Each of the three built-ins reads the array once, so the work grows in step with the number of numbers. Nothing is sorted and no copy of the array is made. The numbers are small (at most 100 values between -1000 and 1000), so the total never gets close to the limit where a TypeScript number stops being exact.

    Some contests score this way: the lowest and the highest scores are dropped, so that one very strict or very kind judge can't decide the result alone. That is the idea of a trimmed total: it ignores the two extremes and keeps the rest.

    Testing

    numsResult
    [7, 9, 6, 8, 10]24
    [5, 1, 5, 9, 1]11
    [-4, 3, -2]-2

    These are the results of calling the method on the statement's examples.

    Common mistakes

    • Forgetting the dots. Math.min(nums) is reported as a type error, and at run time an array of several numbers gives NaN.
    • Dropping every copy. Filtering out all numbers equal to the smallest removes both 1s in the second example and gives 10, not 11.
    • Leaving out the start value of reduce. With 3 or more numbers it still works, but passing 0 is the safe habit.
    • Mixing up the signs. The two extremes are subtracted, not added.
    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free