Start learning free

LESSON 118 OF 289 · ARRAYS - CHALLENGES

Middle Element in TypeScript: Math.floor and Array Indexes

Practice Read the full explanation

Given an array of integers nums with an odd number of items, return the item in the middle.

The middle index is the length of nums divided by 2, rounded down. For example, 5 items have the indexes 0 to 4, and 5 divided by 2, rounded down, is 2: two items come before index 2 and two come after it.

 

Example 1:

Input: nums = [1, 2, 3]
Output: 2
Explanation: The length is 3, and 3 divided by 2, rounded down, is 1. The item at index 1 is 2.

Example 2:

Input: nums = [5, 9, 1, 8, 4]
Output: 1
Explanation: The middle index is 2, and the item there is 1. The middle is about the position, not the size of the number.

Example 3:

Input: nums = [7]
Output: 7
Explanation: 1 divided by 2, rounded down, is 0: the only item is the middle one.

 

Constraints:

  • nums has an odd number of items: 1, 3, 5 and so on.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · ARRAYS - CHALLENGES

    Middle Element in TypeScript, explained

    Problem overview

    The method middleElement receives an array of integers, nums, with an odd number of items, and returns the item in the middle. For [1, 2, 3] that is 2. For [5, 9, 1, 8, 4] it is 1: the middle is about the position of an item, not the size of its value. For [7] the only item is the middle one.

    Approach

    With an odd number of items, there is exactly one item with the same number of items before it and after it. With 5 items, the indexes are 0 to 4, and index 2 has two items on each side. The index is the length divided by 2, rounded down: 5 / 2 is 2.5, and rounded down it is 2.

    Rounding down matters because division in TypeScript doesn't produce a whole number. The type number is a decimal type, so 7 / 2 is 3.5, and an array can't be read at the index 2.5: nums[2.5] is undefined. Math.floor rounds a number down to the nearest whole number and fixes that.

    Step by step

    1. Find the middle index. Divide nums.length by 2 and round down with Math.floor. Store the result in a constant middle, declared with const and the type number.
    2. Return the item at that index: nums[middle].

    The solution in TypeScript

    class Solution {
        middleElement(nums: number[]): number {
            const middle: number = Math.floor(nums.length / 2);
            return nums[middle];
        }
    }
    

    Some notes on the code:

    • Division gives a decimal. For 5 items, nums.length / 2 is 2.5. Reading the array at that position gives undefined, so the rounding step is required.
    • Math.floor rounds toward negative infinity. For a length, which is never negative, Math.trunc, which drops the decimal part, gives the same result. The two differ only for negative numbers: Math.floor(-2.5) is -3 and Math.trunc(-2.5) is -2.
    • Named constant. Putting the index in middle gives the step a name and keeps the return line short.

    Why integer division needs help

    Some languages divide two integers and give an integer. TypeScript doesn't: every number is a double, and / always gives the exact quotient, so 5 / 2 is 2.5 and 1 / 2 is 0.5. Whenever an index comes from a division, round it explicitly, as the lesson does.

    Tracing the example

    numslengthlength / 2middleresult
    [1, 2, 3]31.512
    [5, 9, 1, 8, 4]52.521
    [7]10.507
    [-3, 0, 8, 2, 6, 10, 4]73.532

    In each row, the number of items before the middle index equals the number after it.

    Edge cases and common mistakes

    • Skipping the rounding. nums[nums.length / 2] reads index 2.5 for 5 items and gives undefined.
    • Subtracting 1 by habit. The last index is length - 1, but the middle index is Math.floor(length / 2). For 5 items the middle index is 2, not 1 or 3.
    • Returning the middle index. The task asks for the item at that position. For [5, 9, 1, 8, 4] the answer is 1, not 2.
    • Picking the middle by value. The middle is a position. The median would need a sorted array, which this lesson doesn't ask for.
    • Even lengths. The lesson only gives odd lengths. With an even number of items there are two middle positions, and this code would return the later of them.

    Testing

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