Array Deletion at Middle Index in DSA Typescript - Time & Space Complexity
When we delete an element from the middle of an array, the time it takes depends on how many elements need to move.
We want to know how the work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
function deleteMiddle(arr: number[]): number[] {
const mid = Math.floor(arr.length / 2);
for (let i = mid; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr.pop();
return arr;
}
This code removes the middle element by shifting all elements after it one step left, then removes the last duplicate.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop shifting elements after the middle index.
- How many times: About half of the array length, since it moves elements from the middle to the end.
As the array grows, the number of elements to shift grows roughly half as fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 shifts |
| 100 | About 50 shifts |
| 1000 | About 500 shifts |
Pattern observation: The work grows linearly with the size of the array.
Time Complexity: O(n)
This means the time to delete the middle element grows in direct proportion to the array size.
[X] Wrong: "Deleting an element from the middle is always fast and takes constant time."
[OK] Correct: Because after deletion, elements must shift to fill the gap, which takes time proportional to how many elements are after the deleted one.
Understanding how array operations scale helps you choose the right data structure and explain your choices clearly in interviews.
"What if we changed the array to a linked list? How would the time complexity of deleting the middle element change?"
Practice
Example: [1, 2, 3, 4, 5] Delete the middle element.Solution
Step 1: Identify the middle index of the array
The array has 5 elements, so middle index = floor(5/2) = 2.Step 2: Remove the element at the middle index
Removing element at index 2 removes the value 3 from the array.Final Answer:
Remove the element at index 2 (value 3) from the array -> Option AQuick Check:
Middle index = 2, remove element 3 [OK]
- Removing first or last element instead of middle
- Using length/2 without floor for odd length
- Confusing index with value
arr?Solution
Step 1: Identify correct method to remove element by index in TypeScript
TypeScript arrays usesplice(index, count)to remove elements at a specific index.Step 2: Use Math.floor(arr.length / 2) to get middle index
This expression correctly calculates the middle index for deletion.Final Answer:
arr.splice(Math.floor(arr.length / 2), 1); -> Option BQuick Check:
splice + floor middle index = correct deletion [OK]
- Using non-existent methods like delete or removeAt
- Using pop() which removes last element only
- Not flooring the middle index calculation
let arr = [10, 20, 30, 40, 50]; arr.splice(Math.floor(arr.length / 2), 1); console.log(arr);
Solution
Step 1: Calculate middle index
Array length is 5, so middle index = floor(5/2) = 2.Step 2: Remove element at index 2
Element at index 2 is 30, so removing it results in [10, 20, 40, 50].Final Answer:
[10, 20, 40, 50] -> Option DQuick Check:
Removed element 30 at index 2 [OK]
- Not flooring index, causing wrong removal
- Removing wrong element index
- Expecting original array unchanged
let arr = [5, 10, 15, 20]; arr.splice(arr.length / 2, 1); console.log(arr);
Solution
Step 1: Check middle index calculation
arr.length / 2 = 4 / 2 = 2 (number), but for even length arrays, middle index should be floor(4/2) = 2, so this seems correct but in TypeScript splice expects integer index.Step 2: Verify if index is integer
arr.length / 2 is 2 (integer), so no error here. But if length was odd, it would be float. The main issue is that for even length arrays, middle is ambiguous; usually floor is used to pick index 2.Step 3: Identify actual problem
In this code, no floor is used but division results in integer 2, so no error. However, if length was odd, index would be float causing error. So best practice is to use Math.floor to ensure integer index.Final Answer:
Using arr.length / 2 without floor causes wrong index -> Option CQuick Check:
Always floor middle index for splice [OK]
- Not flooring index causing float error
- Thinking splice removes all elements
- Misunderstanding console.log usage
arr of even length, how can you delete the lower middle element?Example:
arr = [2, 4, 6, 8] After deletion, output should be [2, 6, 8].Solution
Step 1: Understand middle elements in even length array
For even length 4, middle indices are 1 and 2 (0-based). Lower middle is index 1.Step 2: Calculate index to delete
Math.floor(4/2) = 2, subtract 1 to get lower middle index = 1.Step 3: Use splice to delete element at index 1
arr.splice(1, 1) removes element 4, resulting in [2, 6, 8].Final Answer:
arr.splice(Math.floor(arr.length / 2) - 1, 1); -> Option AQuick Check:
Lower middle index = floor(length/2)-1 [OK]
- Deleting upper middle instead of lower middle
- Not adjusting index for zero-based arrays
- Using ceil instead of floor
Ready to prove it?
Pass the test and this pattern moves unknown
