Array Deletion at End in DSA Go - Time & Space Complexity
We want to understand how fast it is to remove an item from the end of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
package main
func deleteAtEnd(arr []int) []int {
if len(arr) == 0 {
return arr
}
return arr[:len(arr)-1]
}
This code removes the last element from the array by slicing it to exclude the last item.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Slicing the array to exclude the last element.
- How many times: This operation happens once per deletion.
Removing the last element does not require checking or moving other elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter how big the array is.
Time Complexity: O(1)
This means removing the last item takes the same small amount of time, no matter how big the array is.
[X] Wrong: "Removing the last element takes longer if the array is bigger because it has to move all elements."
[OK] Correct: Actually, removing from the end just changes the array length; it does not move or check other elements.
Knowing that deleting from the end is very fast helps you choose the right data structure and operation in real problems.
"What if we changed to deleting an element from the start of the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand array deletion at end
Deleting at the end means removing the last element only, not the first or all.Step 2: Effect on array size
After removal, the array size decreases by one because one element is gone.Final Answer:
The last element is removed and the array size decreases by one. -> Option AQuick Check:
Deleting end element reduces size by one [OK]
- Thinking first element is removed
- Assuming array size stays same
- Believing array clears completely
arr?Solution
Step 1: Understand slice slicing syntax
To remove last element, slice up to length minus one: arr[:len(arr)-1].Step 2: Check other options
arr[len(arr):] results in empty slice, arr[1:] removes first element, arr[:len(arr)+1] causes runtime error.Final Answer:
arr = arr[:len(arr)-1] -> Option BQuick Check:
Slice up to len-1 removes last element [OK]
- Using arr[1:] removes first element
- Using arr[len(arr):] gives empty slice
- Using arr[:len(arr)+1] causes error
arr := []int{10, 20, 30, 40}
arr = arr[:len(arr)-1]
fmt.Println(arr)Solution
Step 1: Initial array and slicing
arr starts as [10 20 30 40]. Slicing arr[:len(arr)-1] means arr[:3].Step 2: Result after slicing
arr[:3] gives first three elements: [10 20 30].Final Answer:
[10 20 30] -> Option CQuick Check:
Slice up to len-1 excludes last element [OK]
- Expecting last element to remain
- Confusing arr[1:] with arr[:len(arr)-1]
- Thinking output is empty slice
arr:arr := []int{1, 2, 3}
arr = arr[:len(arr)]
fmt.Println(arr)Solution
Step 1: Analyze slicing bounds
arr[:len(arr)] means slice from start to full length, so no element is removed.Step 2: Effect on array
Since slice covers entire array, arr remains [1 2 3] unchanged.Final Answer:
It does not delete any element; array remains unchanged. -> Option DQuick Check:
Slicing arr[:len(arr)] keeps full array [OK]
- Assuming last element is deleted
- Expecting runtime error
- Thinking first element is removed
arr := []int{5, 10, 15, 20, 25}, write the Go code to delete the last two elements and print the resulting slice.Solution
Step 1: Understand deletion of last two elements
To remove last two elements, slice up to length minus two: arr[:len(arr)-2].Step 2: Apply slicing and print
arr[:3] gives [5 10 15]. Printing arr shows these three elements.Final Answer:
arr = arr[:len(arr)-2] fmt.Println(arr) -> Option AQuick Check:
Slice up to len-2 removes last two elements [OK]
- Using arr[2:] removes first two elements
- Removing only last element instead of two
- Incorrect slice bounds causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
