2.1Array Operations
Deleting from the front
Deleting the first element of an array is not like deleting from the end. The first cell cannot stay empty, because the first element of an array is always at index 0. So every remaining element is shifted one cell to the left. This lesson shows how the shift works and in which direction it runs.
- Why the elements are shifted to the left when deleting from the front
- Why the shift runs from front to back, and where the loop stops
- Why the operation is O(n), and how it differs from its O(1) linked list counterpart
1Idea
In an array an element's position is its index. When element 0 is deleted, element 1 must take its place, element 2 must take the place of element 1, and so on. The loop starts at i = 0 and writes arr[i] = arr[i + 1] at each step. The last step is i = size - 2: that step moves the last element one to the left.
The direction matters. Going from front to back, each cell is written while the value to its right is still unchanged. The first step writes over the deleted value, which is exactly what we want.
When the shift is done, the last element sits in two cells: at indices size - 2 and size - 1. Decreasing size by one leaves the copy at the end outside the array, just as in deleting from the end. If the array is empty, the function changes nothing.
In Python, list.pop(0) does the same job in one line, but under the hood it still shifts every element. The cost is the same: O(n).
2Step by step
The drawing deletes the first element (12) from an array holding 12, 7, 3, 9 and 5. The capacity is 8; the numbers under the cells are indices.
- Start.
sizeis 5; the element to delete is 12, at index 0. - The array is not empty. The loop will run from
i = 0tosize - 2 = 3. i = 0:arr[0] = arr[1]. 7 is written over 12. Cell 1 still holds the old copy of 7; the next step writes over it.i = 1: 3 moves to cell 1.i = 2: 9 moves to cell 2.i = 3: 5 moves to cell 3. The loop is done. 5 is now in two cells: 3 and 4.sizegoes down by one and becomes 4. The copy in cell 4 is now outside the array.- Result: 7, 3, 9, 5. Four elements moved.
#define CAPACITY 8 bool deleteFromFront(int arr[], int* size) { if (*size == 0) return false; // empty array for (int i = 0; i < *size - 1; i++) arr[i] = arr[i + 1]; // shift one to the left (*size)--; return true;}int deleteFromFront(int[] arr, int size) { if (size == 0) return size; // empty array: unchanged for (int i = 0; i < size - 1; i++) arr[i] = arr[i + 1]; // shift one to the left return size - 1;}def delete_from_front(arr, size): if size == 0: # empty array: unchanged return size for i in range(size - 1): arr[i] = arr[i + 1] # shift one to the left return size - 1The snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
In an array of n elements, n − 1 elements shift one to the left.
The shift is done in place; only the loop counter is used.
Compare: deleting from the head of a linked list is O(1), because only head moves to the next node. In an array the position of the first element is fixed, so every remaining element pays the price.
4Common mistake
A common mistake is forgetting to decrease size after the shift:
for (int i = 0; i < *size - 1; i++) arr[i] = arr[i + 1];return true; // no (*size)--The elements shift correctly, but size stays 5. The array reads 7, 3, 9, 5, 5: the last element is counted twice. Shifting alone does not delete an element; decreasing size does.
5Try it
How many arr[i] = arr[i + 1] assignments does deleting from the front of a 1000-element array take?
The answer is B. The loop runs from i = 0 to i = 998: 999 assignments. The deleted element itself is not moved; it is written over.
Show the answer
The answer is B. The loop runs from i = 0 to i = 998: 999 assignments. The deleted element itself is not moved; it is written over.
Why does the shift run from front to back?
The answer is A. Going from back to front writes arr[3] = arr[4] first, then arr[2] = arr[3], and so on. The last value gets copied into every cell. Going from left to right, each value is read before it is written over.
Show the answer
The answer is A. Going from back to front writes arr[3] = arr[4] first, then arr[2] = arr[3], and so on. The last value gets copied into every cell. Going from left to right, each value is read before it is written over.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.