Start learning free

LESSON 122 OF 675 · ARRAYS - CHALLENGES

Drop the Ends in TypeScript: Slicing the Middle

Practice Read the full explanation

You get an array of integers nums. Return an array with every number except the first and the last.

The numbers in between keep their order. When nums has only two numbers, return an empty array.

 

Example 1:

Input: nums = [4, 8, 15, 16, 23]
Output: [8, 15, 16]
Explanation: 4 is the first number and 23 is the last.
Both are left out.

Example 2:

Input: nums = [9, 0, 9]
Output: [0]
Explanation: Both 9s are ends.
Only the 0 is between them.

Example 3:

Input: nums = [1, 2]
Output: []
Explanation: 1 and 2 are the ends.
Nothing is between them.

 

Constraints:

  • nums has 2 to 10 numbers.
  • Every number is an integer from -1000 to 1000.
STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · ARRAYS - CHALLENGES

    Drop The Ends in TypeScript, explained

    Problem overview

    dropEnds receives an array of integers nums with at least two numbers. It returns a new array with every number except the first and the last, keeping the order of the rest. With exactly two numbers there is nothing in between, so the result is an empty array.

    For [4, 8, 15, 16, 23] the first number is 4 and the last is 23, so the answer is [8, 15, 16]. For [9, 0, 9] both 9s are ends and only [0] is left. For [1, 2] the answer is [].

    Approach

    Use slice with both cut points moved inwards. Starting at index 1 skips the first number. The last index of the array is nums.length - 1, and because slice stops just before its end index, using nums.length - 1 as the end leaves the last number out too. Everything between is copied in order.

    Step by step

    1. Slice from 1 to nums.length - 1 and store the copy in a constant named middle of type number[].
    2. Return middle. With two numbers the call is slice(1, 1), which is empty, so the "two numbers" rule needs no special case.

    The solution in TypeScript

    class Solution {
        dropEnds(nums: number[]): number[] {
            const middle: number[] = nums.slice(1, nums.length - 1);
            return middle;
        }
    }

    On the three examples it returns:

    [ 8, 15, 16 ] [ 0 ] []

    Tracing the first example

    For [4, 8, 15, 16, 23] the length is 5, so the call is slice(1, 4). Index 1 holds 8, index 2 holds 15 and index 3 holds 16, and the copy stops before index 4, which holds 23. The result has 4 - 1 = 3 items. In general the result has nums.length - 2 items, which is why two numbers give none.

    The function never needs to look at the values. It works only with positions, so it behaves the same for negative numbers and for repeated numbers such as the two 9s in [9, 0, 9].

    Edge cases and common mistakes

    • Two numbers. slice(1, 1) gives [], as the lesson requires.
    • Three numbers. slice(1, 2) gives exactly the one number in the middle.
    • Using nums.length as the end. That would keep the last number. The end must be one less.
    • Starting at 0. That would keep the first number.
    • Negative indexes. slice(1, -1) means the same thing, because a negative end counts from the back: [1, 2, 3, 4].slice(1, -1) gives [ 2, 3 ] and [1, 2].slice(1, -1) gives []. The lesson's form with nums.length - 1 spells the same cut out in plain numbers.
    • Removing with shift and pop. shift removes the first item and pop removes the last, so calling both on nums does produce [ 8, 15, 16 ] for the first example. But it changes the input array: after the call, nums itself is [ 8, 15, 16 ]. To use them safely, copy first with nums.slice() and remove from the copy. The lesson's single slice(1, nums.length - 1) does the same work in one step and leaves nums alone.

    Testing

    Check an even length, negative values and the largest values the constraints allow: dropEnds([5, 6, 7, 8]) returns [ 6, 7 ], dropEnds([-3, 0, 3]) returns [ 0 ] and dropEnds([1000, -1000]) returns []. In every case the answer is two items shorter than the input, never less than empty.

    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free