Array Deletion at Beginning in DSA Typescript - Time & Space Complexity
When we remove the first item from an array, the computer must adjust the rest of the items.
We want to know how the time to do this changes as the array gets bigger.
Analyze the time complexity of the following code snippet.
const arr = [1, 2, 3, 4, 5];
arr.shift(); // removes the first element
console.log(arr);
This code removes the first element from the array and shifts all other elements left by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Shifting all elements after the first one to the left by one position.
- How many times: For an array of size n, this shifting happens n-1 times.
As the array grows, the number of elements to shift grows almost the same.
| Input Size (n) | Approx. Operations (shifts) |
|---|---|
| 10 | 9 |
| 100 | 99 |
| 1000 | 999 |
Pattern observation: The operations grow roughly in a straight line with the size of the array.
Time Complexity: O(n)
This means the time to delete the first element grows directly with the number of items in the array.
[X] Wrong: "Removing the first element is always fast and constant time like removing the last element."
[OK] Correct: Because after removing the first element, all other elements must move one step left, which takes time proportional to the array size.
Understanding this helps you explain why some operations on arrays can be slower than expected and when to choose other data structures like linked lists.
"What if we changed the array to a linked list? How would the time complexity of deleting the first element change?"
Practice
array.shift() do to the array?Solution
Step 1: Understand
Theshift()methodshift()method removes the first element of the array.Step 2: Effect on array elements
After removal, all other elements move one position to the left to fill the gap.Final Answer:
Removes the first element and shifts all other elements left -> Option BQuick Check:
shift()removes first element [OK]
- Confusing shift() with pop()
- Thinking shift() deletes entire array
- Assuming shift() adds elements
nums in TypeScript?Solution
Step 1: Identify the method to remove first element
The correct method to remove the first element in TypeScript arrays isshift().Step 2: Check other options
pop()removes last element,removeFirst()anddelete(0)are invalid methods.Final Answer:
nums.shift() -> Option AQuick Check:
Useshift()to remove first element [OK]
- Using pop() instead of shift()
- Using non-existent methods like removeFirst()
- Trying to delete with delete(0) which is invalid
let arr = [10, 20, 30, 40]; arr.shift(); console.log(arr);
Solution
Step 1: Initial array and shift operation
The array starts as [10, 20, 30, 40]. Callingshift()removes the first element 10.Step 2: Resulting array after shift
After removal, the array becomes [20, 30, 40].Final Answer:
[20, 30, 40] -> Option DQuick Check:
shift() removes first element, rest remain [OK]
- Expecting original array unchanged
- Thinking shift() removes last element
- Confusing output with pop() effect
let numbers = [5, 10, 15]; numbers.shift; console.log(numbers);
What is the issue and how to fix it?
Solution
Step 1: Identify the method call mistake
The code usesnumbers.shiftwithout parentheses, so the method is not called.Step 2: Correct method call syntax
Adding parenthesesnumbers.shift()calls the method and removes the first element.Final Answer:
Missing parentheses after shift; fix by using numbers.shift() -> Option AQuick Check:
Method calls need parentheses [OK]
- Forgetting parentheses on method calls
- Using pop() instead of shift() incorrectly
- Misunderstanding shift() removes first element
let fruits = ['apple', 'banana', 'cherry', 'date'];. You want to remove the first two fruits efficiently. Which code snippet correctly does this?Solution
Step 1: Understand removing multiple elements
fruits.shift(2);fruits.shift(2);is incorrect: shift() takes no arguments (ignores 2) and removes only the first element.Step 2: Using splice for efficient removal
splice(0, 2)removes two elements starting at index 0 in one operation.Step 3: Check other options
pop()removes from end,delete()is invalid for arrays.Final Answer:
fruits.splice(0, 2); -> Option CQuick Check:
Use splice to remove multiple from start efficiently [OK]
- Using shift(2) which only removes one element
- Using pop() which removes from end
- Using invalid delete() method
Ready to prove it?
Pass the test and this pattern moves unknown
