Start learning free

LESSON 117 OF 289 · ARRAYS - CHALLENGES

Swap First and Last in TypeScript: In-Place Array Swap

Practice Read the full explanation

Given an array of integers nums, swap its first and last numbers and return it.

Change nums itself, then return it. The numbers in between stay where they are. When nums has only one number, that number is both the first and the last, so it stays as it is.

 

Example 1:

Input: nums = [1, 2, 3, 4, 5]
Output: [5, 2, 3, 4, 1]
Explanation: 1 and 5 trade places. 2, 3 and 4 don't move.

Example 2:

Input: nums = [4, 9]
Output: [9, 4]
Explanation: With two numbers, the first and the last are next to each other, and they trade places.

Example 3:

Input: nums = [7]
Output: [7]
Explanation: One number is both the first and the last, so nothing changes.

 

Constraints:

  • nums has at least one number.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · ARRAYS - CHALLENGES

    Swap First And Last in TypeScript, explained

    Problem overview

    The method swapFirstAndLast receives an array of integers, nums, swaps its first and last numbers and returns it. The array itself is changed, and the numbers between the two ends stay where they are. [1, 2, 3, 4, 5] becomes [5, 2, 3, 4, 1]. With two numbers, [4, 9] becomes [9, 4]. With one number, [7], the first and the last are the same item, so nothing changes.

    Approach

    Two values can't trade places in a single move. Suppose you first write the last number over the first place: the old first number is gone. The fix is a temporary variable that keeps the first number safe while the first place is overwritten, like a third cup when you swap the drinks in two cups.

    The first number is at index 0 and the last one is at index nums.length - 1, because indexes start at 0.

    Step by step

    1. Save the first number. Store nums[0] in a constant temp, declared with const and the type number. The next step overwrites nums[0], so its old value must be saved now.
    2. Copy the last number into the first place. Set nums[0] to nums[nums.length - 1]. For [1, 2, 3, 4, 5] the array is now [5, 2, 3, 4, 5].
    3. Put the saved number in the last place. Set nums[nums.length - 1] to temp. The array is now [5, 2, 3, 4, 1].
    4. Return nums. The array itself was changed, so it comes back swapped.

    The solution in TypeScript

    class Solution {
        swapFirstAndLast(nums: number[]): number[] {
            const temp: number = nums[0];
            nums[0] = nums[nums.length - 1];
            nums[nums.length - 1] = temp;
            return nums;
        }
    }
    

    Some details of the code:

    • Arrays are changed in place. The function receives a reference to the caller's array, not a copy. After let arr = [1, 2, 3] and a call with arr, the variable arr itself holds [3, 2, 1], and the returned value is the very same array: comparing the two with === gives true. To keep the original, copy first, for example with [...nums].
    • const is enough for temp. It is assigned once and never changes. The array's items are assigned, not temp, and assigning an item is allowed even when the array was passed in as a parameter.
    • The item type is number. The annotation on temp is erased when the code compiles, so it costs nothing at run time.

    Tracing the example

    With [1, 2, 3, 4, 5], temp is 1. After the second line the array is [5, 2, 3, 4, 5]. After the third it is [5, 2, 3, 4, 1]. With one number, [7], both indexes are 0: temp is 7, the array receives its own item, then temp, and stays [7].

    Another way to swap

    Destructuring swaps without a temporary variable: [nums[0], nums[nums.length - 1]] = [nums[nums.length - 1], nums[0]]; turns [1, 2, 3] into [3, 2, 1]. The lesson's version spells out each move, which shows why a temporary value is needed.

    Edge cases and common mistakes

    • Skipping the temporary variable. Writing nums[0] = nums[nums.length - 1] and then nums[nums.length - 1] = nums[0] copies the last number twice. For [1, 2, 3] the result is [3, 2, 3]: the 1 is lost.
    • Using nums.length as the last index. That index is past the end. The last index is nums.length - 1.
    • One-element arrays. No special case is needed. The first and the last index are both 0, and the swap leaves the array unchanged.
    • Two-element arrays. The two items are next to each other and simply trade places: [4, 9] becomes [9, 4].

    Testing

    • [1, 2, 3, 4, 5] gives [5, 2, 3, 4, 1].
    • [4, 9] gives [9, 4].
    • [7] gives [7].
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free