0
0
DSA Pythonprogramming

Array Deletion at Middle Index in DSA Python

Choose your learning style9 modes available
Mental Model
To delete an element from the middle of an array, we shift all elements after it one step left to fill the gap.
Analogy: Imagine a row of chairs where one person leaves from the middle; everyone behind moves one seat forward to close the empty spot.
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], delete element at index 2
Goal: Remove the element at index 2 and shift remaining elements left
Step 1: Start deletion at index 2 (value 3)
1 -> 2 -> [3↑] -> 4 -> 5
Why: We identify the element to remove
Step 2: Shift element at index 3 (value 4) to index 2
1 -> 2 -> 4 -> [4] -> 5
Why: Fill the gap by moving next element left
Step 3: Shift element at index 4 (value 5) to index 3
1 -> 2 -> 4 -> 5 -> [5]
Why: Continue shifting elements left to close gap
Step 4: Remove last duplicate element at index 4
1 -> 2 -> 4 -> 5 -> null
Why: Last element is now duplicate, so reduce array size
Result:
1 -> 2 -> 4 -> 5 -> null
Annotated Code
DSA Python
class Array:
    def __init__(self, elements):
        self.arr = elements
        self.size = len(elements)

    def delete_at_index(self, index):
        if index < 0 or index >= self.size:
            print("Index out of bounds")
            return
        for i in range(index, self.size - 1):
            self.arr[i] = self.arr[i + 1]  # shift element left
        self.size -= 1  # reduce size to remove last duplicate

    def print_array(self):
        for i in range(self.size):
            print(self.arr[i], end=" -> " if i < self.size - 1 else " -> null\n")

# Driver code
array = Array([1, 2, 3, 4, 5])
array.delete_at_index(2)
array.print_array()
if index < 0 or index >= self.size:
check if index is valid to avoid errors
for i in range(index, self.size - 1):
loop to shift elements left starting from deletion index
self.arr[i] = self.arr[i + 1] # shift element left
move element from right to current position to fill gap
self.size -= 1 # reduce size to remove last duplicate
decrease size to logically remove last duplicate element
OutputSuccess
1 -> 2 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because in worst case we shift all elements after the deleted index once
Space: O(1) because deletion is done in place without extra storage
vs Alternative: Compared to creating a new array without the element (O(n) time and space), this method saves space by shifting in place
Edge Cases
delete at index 0 (first element)
All elements shift left, first element removed
DSA Python
if index < 0 or index >= self.size:
delete at last index
No shifting needed, just reduce size by one
DSA Python
for i in range(index, self.size - 1):
delete with invalid index (negative or too large)
Prints 'Index out of bounds' and no change
DSA Python
if index < 0 or index >= self.size:
When to Use This Pattern
When you need to remove an element from the middle of a fixed-size list or array, think of shifting elements left to fill the gap.
Common Mistakes
Mistake: Not reducing the size after shifting, leaving a duplicate at the end
Fix: Decrease the size variable by one after shifting elements
Mistake: Trying to shift elements beyond array bounds
Fix: Ensure loop runs only until size - 1 to avoid index errors
Summary
Deletes an element at a given index by shifting subsequent elements left.
Use when you want to remove an element from the middle of an array without creating a new array.
The key is to move all elements after the deleted one one step left and reduce the array size.