Array Deletion at End in DSA C# - 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 grows bigger?
Analyze the time complexity of the following code snippet.
// Remove last element from array
int[] arr = new int[] {1, 2, 3, 4, 5};
int length = arr.Length;
if (length > 0) {
length = length - 1; // simulate deletion by reducing length
}
// Now array is considered to have one less element
This code simulates deleting the last element by reducing the length count.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single step to reduce length by one.
- How many times: Exactly once per deletion.
Removing the last element 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 the same even if the array grows larger.
Time Complexity: O(1)
This means deleting the last element takes a constant time, no matter the array size.
[X] Wrong: "Deleting an element at the end takes longer as the array grows."
[OK] Correct: Because we only reduce the length count, no shifting or searching is needed.
Knowing that removing from the end is fast helps you choose the right data structure and explain your choices clearly.
"What if we tried to delete an element from the start of the array instead? 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.Step 2: Effect on array size
Removing the last element reduces the array size by one.Final Answer:
The last element is removed and the array size decreases by one. -> Option DQuick Check:
Delete end element = size - 1 [OK]
- Thinking the first element is removed
- Assuming array size stays the same
- Believing elements shift right
arr?Solution
Step 1: Check valid array deletion methods
Arrays in C# are fixed size; to delete last element, create new array excluding last.Step 2: Analyze options
arr = arr.Take(arr.Length - 1).ToArray(); uses LINQ Take to get all but last element and converts to array, which is correct.Final Answer:
arr = arr.Take(arr.Length - 1).ToArray(); -> Option CQuick Check:
Use Take and ToArray to delete last element [OK]
- Using RemoveAt on array (only for List)
- Calling Pop() on array (not supported)
- Using invalid Delete method
int[] arr = {10, 20, 30, 40};
arr = arr.Take(arr.Length - 1).ToArray();
Console.WriteLine(string.Join(", ", arr));Solution
Step 1: Understand the Take method
arr.Take(arr.Length - 1) takes first 3 elements: 10, 20, 30.Step 2: Convert to array and print
After conversion, arr has 3 elements: 10, 20, 30, printed with commas.Final Answer:
10, 20, 30 -> Option AQuick Check:
Take length-1 elements = 10, 20, 30 [OK]
- Printing original array without deletion
- Confusing which elements are taken
- Assuming last element remains
int[] arr = {1, 2, 3, 4};
arr.RemoveAt(arr.Length - 1);Solution
Step 1: Check array methods
Arrays in C# do not support RemoveAt; it's a List method.Step 2: Correct approach
To delete last element from array, create new array or use List instead.Final Answer:
Arrays do not have RemoveAt method; use List instead. -> Option AQuick Check:
RemoveAt not in array, only List [OK]
- Trying RemoveAt on array
- Wrong index for RemoveAt
- Assuming RemoveAt needs two parameters
int[] arr = {5, 10, 15, 20, 25};. Write C# code to delete the last two elements and print the resulting array.Solution
Step 1: Remove last two elements using Take
Take(arr.Length - 2) keeps first 3 elements: 5, 10, 15.Step 2: Convert to array and print
Convert to array and print elements separated by commas.Final Answer:
arr = arr.Take(arr.Length - 2).ToArray(); Console.WriteLine(string.Join(", ", arr)); -> Option BQuick Check:
Take length-2 elements = 5, 10, 15 [OK]
- Using RemoveRange on array (only for List)
- Using Skip to remove from start instead of end
- Taking wrong number of elements
Ready to prove it?
Pass the test and this pattern moves unknown
