algoritmauzmanı
Sign in
TREN
2.1Array Operations
Lesson 2.1.6

Deleting from the end

Deleting the element at the end of an array is the shortest array operation. No element moves; only size, which holds the length of the filled part, goes down by one. This lesson explains why the deleted value stays in memory and why that is not a problem.

What you will learn
  • Why deleting from the end is nothing more than decreasing size by one
  • What it means that the deleted value stays in memory, and why the operation is
  • How an empty array is handled, and how the operation is written in C, Java and Python
4 minTime O(1)Memory O(1)C · Java · Python

1Idea

An has a fixed capacity: CAPACITY cells are set aside up front. A separate size variable tells which part of the array is filled. The valid elements sit at 0 to size - 1; size and everything after it count as empty.

The last element is at index size - 1. To delete it, decreasing size by one is enough. The value in the cell is not erased and stays in memory. But it is no longer part of the array: traversing, searching and printing only look up to size. The next insert at the end writes over that cell.

If the array is empty (size == 0) there is nothing to delete. The C function returns false; the Java and Python versions return size unchanged. Without this check size would become -1 and every later operation would break.

The (void)arr; line in C tells the compiler that we leave the parameter unused on purpose. The function still takes the array so that every array operation is called the same way.

2Step by step

The drawing deletes the last element of a five-element array holding 12, 7, 3, 9 and 5. The capacity is 8; the numbers under the cells are indices. The box on the right is the size variable.

012345671273955size4size
  1. Start. size is 5: the valid elements are at indices 0 to 4. Cells 5, 6 and 7 are empty.
  2. size is not 0, so the array is not empty. The last element is 5, at index 4.
  3. size goes down by one and becomes 4. 5 is still in cell 4, but it is now outside the array.
  4. Result: 12, 7, 3, 9. No element moved; the only thing that changed is size.

#define CAPACITY 8 bool deleteFromEnd(int arr[], int* size) {    (void)arr;  // the array itself is not touched    if (*size == 0) return false;  // empty array    (*size)--;  // the last element is now outside the array    return true;}
int deleteFromEnd(int[] arr, int size) {    if (size == 0) return size;  // empty array: unchanged    return size - 1;  // the last element is now outside the array}
def delete_from_end(arr, size):    if size == 0:  # empty array: unchanged        return size    return size - 1  # the last element is now outside the array

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

3Complexity

Time
O(1)

Independent of the number of elements: one comparison and one decrement.

Memory
O(1)

No extra memory is used.

Compare: deleting from the end of a linked list means you the list up to the second-to-last node, which makes it . In an array the position of the last element is computed from size.

4Common mistake

The most common mistake is forgetting the empty-array check:

bool deleteFromEnd(int arr[], int* size) {    (*size)--;  // becomes -1 when size is 0    return true;}

Called on an empty array, size becomes -1. The next insert at the end writes to arr[-1], that is, it corrupts memory outside the array. A traversal loop simply does not run, so the bug stays hidden. Always check for size == 0 before deleting.

5Try it

After deleting from the end, the old last value stays in memory. Why is that not a problem?

Show the answer

The answer is B. size defines the filled part of the array. The old value stays at index size and no operation looks there. The next value inserted at the end writes over it. A fixed array keeps its capacity; it does not shrink.

What happens if deleteFromEnd is called twice on an array with capacity 8 and size 1?

Show the answer

The answer is A. The first call moves the only element outside the array. On the second call size is 0; the check catches it and the array stays unchanged.

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