Delete Element from Array

Say you have a list of names in an array and one person leaves. You want that name gone. Here is the tricky part. An array stores its elements one after another in memory with no gaps. So when you remove one from the middle you leave a hole, and a hole in the middle is not allowed. To fix it you slide the elements after the hole one step to the left and close the gap. That sliding is what deletion is really about, and that is what we will learn here.

We will go through it the best way possible. Clear steps first, then code in five languages with real output.

πŸ› οΈ Ways to Delete an Element from an Array

Deleting depends on where the element sits. So we will look at these cases:

  1. Delete from the Start
  2. Delete from a Specific Position
  3. Delete from the End

Notice the array never really shrinks in memory. Its capacity stays the same. What changes is the size, which is how many real elements we are keeping. So deleting mostly means shifting elements and then reducing the size by one.

1️⃣ Delete an element from the Start of an Array

To delete the first element, we shift every element one position to the left. The element at index 1 moves to index 0, the one at index 2 moves to index 1, and so on. This overwrites the first element and closes the gap.

Steps to delete an element from the start of an array

Before we write code, let us understand the steps.

  1. Declare two global variables:
    • size β†’ tracks the current number of elements
    • capacity β†’ tracks the maximum size of the array
  2. Create a function deleteFromStart(array).
  3. Inside deleteFromStart:
    • Check if the array is empty (size == 0).
    • If empty, print an error and return.
    • Run a loop from 0 to size - 2 to shift elements one position to the left using: array[i] = array[i + 1]
    • Decrement size by 1.
  4. Inside the main function:
    • Create an array with some elements.
    • Call deleteFromStart for each deletion.
    • Print the array after each deletion.

Why Size and Capacity declared as Global Variables?

  • Capacity: Capacity is fixed for the array and does not change during deletions. So we can declare it as a global variable.
  • Size: Size changes with each deletion. We need to maintain it externally. Otherwise we would have to scan the whole array each time just to know how many real elements are left.

Deleting is just shifting left

Removing an element never frees memory in a fixed-size array. You only slide the later elements left and reduce the size. The slot at the end is still there, it just stops counting.

Code Examples to Delete Element from Start of Array

Now lets see how to implement this in different programming languages.

Select your preferred programming language for code examples: Language

Program to delete element from start of array in Python

DeleteFromStart.py
# Declare global variables for size and capacity
size = 5
capacity = 5
def delete_from_start(arr):
global size
# If array is empty, return error that nothing can be deleted
if size == 0:
print("Array is empty. Cannot delete element.")
return
# Shift elements to the left
for i in range(0, size - 1):
arr[i] = arr[i + 1]
# One less real element now
size -= 1
def main():
# Start with a filled array
arr = [10, 20, 30, 40, 50]
# Delete the first element three times
for _ in range(3):
print("Deleting element from start")
delete_from_start(arr)
# Only the first 'size' elements are real
print("Current array:", arr[:size])
if __name__ == "__main__":
main()

The output of the above code will be:

Deleting element from start
Current array: [20, 30, 40, 50]
Deleting element from start
Current array: [30, 40, 50]
Deleting element from start
Current array: [40, 50]

Complexity Analysis for Deletion from Start of Array

Time Complexity:

  • O(n) - We shift almost every element one position to the left, which takes linear time. Space Complexity:
  • O(1) - We are not using any extra space that grows with input size; we only use a fixed amount of space for variables.

2️⃣ Delete from a Specific Position

To delete an element at a specific position, we shift every element that comes after that position one step to the left. So the element after the position overwrites the one we want gone, and the gap closes.

Steps to delete an element from a specific position

As usual we should first understand the steps.

  1. Declare two global variables:
    • size β†’ tracks the current number of elements
    • capacity β†’ tracks the maximum size of the array
  2. Create a function deleteFromPosition(array, position).
  3. Inside deleteFromPosition:
    • Check if the array is empty (size == 0).
    • If empty, print an error and return.
    • Check if the position is valid (0 <= position < size).
    • If invalid, print an error and return.
    • Run a loop from position to size - 2 to shift elements one position to the left using: array[i] = array[i + 1]
    • Decrement size by 1.
  4. Inside the main function:
    • Create an array with some elements.
    • Call deleteFromPosition for each deletion.
    • Print the array after each deletion.

Check the position carefully

A valid position runs from 0 up to size - 1. If the position is negative, there is no element there. If it is equal to or bigger than the size, there is no element there either. So we stop early with an error instead of touching memory we should not.

Code Examples to Delete Element from Specific Position in Array

Now lets see how to implement this in different programming languages.

Select your preferred programming language for code examples: Language

Program to delete element from specific position of array in Python

DeleteFromPosition.py
# Declare global variables for size and capacity
size = 5
capacity = 5
def delete_from_position(arr, position):
global size
# If array is empty, return error that nothing can be deleted
if size == 0:
print("Array is empty. Cannot delete element.")
return
# Check for valid position
if position < 0 or position >= size:
print("Invalid position. Cannot delete element.")
return
# Shift elements after the position to the left
for i in range(position, size - 1):
arr[i] = arr[i + 1]
# One less real element now
size -= 1
def main():
# Start with a filled array
arr = [10, 20, 30, 40, 50]
# Positions to delete one after another
positions = [2, 0, 5]
for position in positions:
print(f"Deleting element at position {position}")
delete_from_position(arr, position)
# Only the first 'size' elements are real
print("Current array:", arr[:size])
if __name__ == "__main__":
main()

The output of the above code will be:

Deleting element at position 2
Current array: [10, 20, 40, 50]
Deleting element at position 0
Current array: [20, 40, 50]
Deleting element at position 5
Invalid position. Cannot delete element.
Current array: [20, 40, 50]

Complexity Analysis for Deletion from Specific Position of Array

Time Complexity:

  • O(n) - In the worst case the position is the start, so we shift almost every element one place to the left, which takes linear time. Space Complexity:
  • O(1) - We are not using any extra space that grows with input size; we only use a fixed amount of space for variables.

3️⃣ Delete an element from the End of an Array

This one is the easiest. The last element has nothing after it. So no element needs to move. We just reduce the size by one and the last element stops counting as a real element.

Steps to delete an element from the end of an array

As usual we should first understand the steps.

  1. Declare two global variables:
    • size β†’ tracks the current number of elements
    • capacity β†’ tracks the maximum size of the array
  2. Create a function deleteFromEnd(array).
  3. Inside deleteFromEnd:
    • Check if the array is empty (size == 0).
    • If empty, print an error and return.
    • Decrement size by 1. No shifting is needed.
  4. Inside the main function:
    • Create an array with some elements.
    • Call deleteFromEnd for each deletion.
    • Print the array after each deletion.

No shifting at the end

Deleting the last element costs almost nothing. There is nobody behind it to slide forward, so you only drop the size. That is why it is the fastest deletion.

Code Examples to Delete Element from End of Array

Now lets see how to implement this in different programming languages.

Select your preferred programming language for code examples: Language

Program to delete element from end of array in Python

DeleteFromEnd.py
# Declare global variables for size and capacity
size = 5
capacity = 5
def delete_from_end(arr):
global size
# If array is empty, return error that nothing can be deleted
if size == 0:
print("Array is empty. Cannot delete element.")
return
# Just drop the last element by reducing the size
size -= 1
def main():
# Start with a filled array
arr = [10, 20, 30, 40, 50]
# Delete the last element three times
for _ in range(3):
print("Deleting element from end")
delete_from_end(arr)
# Only the first 'size' elements are real
print("Current array:", arr[:size])
if __name__ == "__main__":
main()

The output of the above code will be:

Deleting element from end
Current array: [10, 20, 30, 40]
Deleting element from end
Current array: [10, 20, 30]
Deleting element from end
Current array: [10, 20]

Complexity Analysis for Deletion from End of Array

Time Complexity:

  • O(1) - Deleting from the end takes constant time because no element needs to be shifted. Space Complexity:
  • O(1) - We are not using any extra space that grows with input size; we only use a fixed amount of space for variables.

Time and Space Complexity Table for Deletion in Array

Deletion Type Time Complexity Space Complexity
Delete from Start O(n) O(1)
Delete from Specific Position O(n) O(1)
Delete from End O(1) O(1)

🧩 What You’ve Learned

You can now remove an element from a fixed-size array in any of the three spots. Here is what you picked up.

  • βœ… Deleting from an array means shifting the later elements left to close the gap, then reducing the size.
  • βœ… Deleting from the start shifts every element left, so it costs O(n).
  • βœ… Deleting from a position shifts only the elements after it, and you must check the position is valid first.
  • βœ… Deleting from the end is just reducing the size, so it costs O(1) with no shifting.
  • βœ… The array’s capacity never changes; only the size goes down.
  • βœ… Keeping size as a global variable saves you from scanning the whole array each time.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What happens to the elements after the deleted one when you delete from a specific position?

    Why: To close the gap, every element after the deleted position moves one step to the left.

  2. 2

    Why is deleting from the end of an array O(1)?

    Why: The last element has nothing after it, so no shifting is needed and you just decrement the size.

  3. 3

    Before deleting from a specific position, what should you check?

    Why: A valid position runs from 0 to size - 1; anything outside that range has no real element to delete.

  4. 4

    When you delete an element from a fixed-size array, what changes?

    Why: Capacity is fixed for a fixed-size array; deletion only reduces the size that counts as real elements.

πŸš€ What’s Next?

You can add and remove elements now. Pair this with the related array skills.

  • Insert Element in Array is the mirror operation, where you shift elements right to make space.
  • Array Traversal shows how to visit every element, which you already used to print the array after each deletion.

Share & Connect