algoritmauzmanı
Sign in
TREN
2.1Array Operations
Lesson 2.1.8

Deleting from the middle

Deleting from the middle is the general form of deleting from the front and from the end: it removes the element at a given position. Positions start at 0. Every element to the right of the deleted one shifts one cell to the left; the elements to its left are not touched.

What you will learn
  • Why the shift starts at the deleted position and moves to the right
  • How an invalid position is caught, and why an empty array needs no separate check
  • Why the loop stops at size - 2, and why reading arr[size] is a bug
6 minTime O(n)Memory O(1)C · Java · Python

1Idea

To delete the element at pos, every element to its right shifts one to the left. The loop starts at i = pos and writes arr[i] = arr[i + 1]. The last step is i = size - 2; that step moves the last element one to the left. Then size goes down by one.

Valid positions are 0 to size - 1. If the position is negative, or equal to or greater than size, there is no element to delete; the function returns false and the array stays unchanged. In an empty array size is 0, so every position is invalid; no separate check is needed.

pos = 0 is deleting from the front. pos = size - 1 is deleting from the end: the loop does not run and only size goes down. The cost depends on the position: there is one shift for each element on the right.

Deleting by value

Sometimes you get a value instead of a position: "delete 9". First you find the value's index with a search, then you call this function with that index. If the search returns -1, there is nothing to delete.

int k = search(arr, size, 9);if (k != -1) deleteAt(arr, &size, k);

2Step by step

The drawing deletes the element at position 2 (3) from an array holding 12, 7, 3, 9, 5 and 8. The capacity is 8; the numbers under the cells are indices.

01234567pos = 212739589586size5sizeposi
  1. Start. size is 6; position 2 holds 3.
  2. The position is valid: it lies between 0 and size - 1 = 5. The loop will start at i = pos.
  3. i = 2: arr[2] = arr[3]. 9 is written over 3. Cell 3 still holds the old copy of 9.
  4. i = 3: 5 moves to cell 3.
  5. i = 4: 8 moves to cell 4. For i = 5, i < size - 1 is false; the loop ends.
  6. size goes down by one and becomes 5. The copy of 8 in cell 5 is now outside the array.
  7. Result: 12, 7, 9, 5, 8. The elements at positions 0 and 1 were never touched.

#define CAPACITY 8 bool deleteAt(int arr[], int* size, int pos) {    if (pos < 0 || pos >= *size) return false;  // invalid position    for (int i = pos; i < *size - 1; i++)        arr[i] = arr[i + 1];  // shift one to the left    (*size)--;    return true;}
int deleteAt(int[] arr, int size, int pos) {    if (pos < 0 || pos >= size) return size;  // invalid position    for (int i = pos; i < size - 1; i++)        arr[i] = arr[i + 1];  // shift one to the left    return size - 1;}
def delete_at(arr, size, pos):    if pos < 0 or pos >= size:  # invalid position        return size    for i in range(pos, size - 1):        arr[i] = arr[i + 1]  # shift one to the left    return size - 1

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(n)

The size - pos - 1 elements to the right of the position are shifted. The worst case is position 0: n − 1 shifts.

Memory
O(1)

The shift is done in place; only the loop counter is used.

Deleting at a position in a linked list is also , but there the time goes into finding the previous node; changing the link is a single assignment. In an array the position is reached at once, and the time goes into shifting.

4Common mistake

The most common mistake is shifting in the wrong direction:

for (int i = *size - 1; i > pos; i--)    arr[i - 1] = arr[i];  // wrong direction

Going from right to left writes arr[4] = arr[5] first, then arr[3] = arr[4]. But arr[4] is already 8. The last value is copied into every shifted cell and the array becomes 12, 7, 8, 8, 8. Deletion shifts from left to right, insertion from right to left: each write must go to a cell whose value has already been moved or is being deleted.

The second common mistake is writing the loop as i < *size. The last round reads arr[size]. If the array is full, that index is outside the array: undefined behavior in C, an ArrayIndexOutOfBoundsException in Java and an IndexError in Python.

5Try it

What happens if deleteAt(arr, &size, 6) is called on an array of size 6?

Show the answer

The answer is B. The valid positions are 0 to 5. The check on the first line catches pos >= size. To delete the last element, pass position 5.

In an array of size n, deleting which position needs the fewest shifts?

Show the answer

The answer is C. There is no element to the right of the last position; the loop does not run and only size goes down. Position 0 is the most expensive: n − 1 shifts.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.