Array Deletion at End in DSA C - Time & Space Complexity
We want to understand how fast it is to remove an element 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.
void deleteAtEnd(int arr[], int *size) {
if (*size == 0) return; // no elements to delete
(*size)--;
}
This code removes the last element by reducing the size count of the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Decreasing the size variable by one.
- How many times: Happens once per deletion call, no loops or recursion.
Removing the last element always takes the same small step, no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant regardless of array size.
Time Complexity: O(1)
This means deleting the last element takes the same short time no matter how big the array is.
[X] Wrong: "Deleting from the end takes longer as the array grows because we have to move many elements."
[OK] Correct: We do not move any elements when deleting at the end; we just reduce the size count, so time stays the same.
Knowing that deleting at the end is very fast helps you choose the right data structure and shows you understand how arrays work under the hood.
"What if we changed deletion to remove the first element instead? How would the time complexity change?"
