Bird
0
0
DSA Cprogramming

Array Deletion at Middle Index in DSA C

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 position to the left to fill the gap.
Analogy: Imagine a row of books on a shelf. If you remove one book from the middle, you slide all the books after it to the left to close the empty space.
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
               ↑
           delete here
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 (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 (5) to index 3
1 -> 2 -> 4 -> [5] -> 5
Why: Continue shifting elements left
Step 4: Reduce array size by 1, last element ignored
1 -> 2 -> 4 -> 5 -> null
Why: Array size decreases after deletion
Result:
1 -> 2 -> 4 -> 5 -> null
Annotated Code
DSA C
#include <stdio.h>

void deleteAtIndex(int arr[], int *size, int index) {
    if (index < 0 || index >= *size) {
        printf("Index out of bounds\n");
        return;
    }
    for (int i = index; i < *size - 1; i++) {
        arr[i] = arr[i + 1]; // shift element left to fill gap
    }
    (*size)--; // reduce size after deletion
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i < size - 1) printf(" -> ");
    }
    printf(" -> null\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = 5;
    int deleteIndex = 2;

    printf("Original array:\n");
    printArray(arr, size);

    deleteAtIndex(arr, &size, deleteIndex);

    printf("Array after deleting element at index %d:\n", deleteIndex);
    printArray(arr, size);

    return 0;
}
if (index < 0 || index >= *size) {
check if index is valid to avoid errors
for (int i = index; i < *size - 1; i++) {
loop to shift elements left starting from deletion index
arr[i] = arr[i + 1]; // shift element left to fill gap
move next element to current position to fill gap
(*size)--; // reduce size after deletion
decrease array size after removing element
OutputSuccess
Original array: 1 -> 2 -> 3 -> 4 -> 5 -> null Array after deleting element at index 2: 1 -> 2 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because in the 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 (which uses O(n) space), this method is more space efficient
Edge Cases
delete at index 0 (first element)
All elements shift left, first element removed
DSA C
if (index < 0 || index >= *size) {
delete at last index
No shifting needed, just reduce size
DSA C
for (int i = index; i < *size - 1; i++) {
delete with invalid index (negative or >= size)
Print error message, no change to array
DSA C
if (index < 0 || index >= *size) {
When to Use This Pattern
When you need to remove an element from the middle of a fixed-size list and maintain order, use array shifting to fill the gap.
Common Mistakes
Mistake: Not reducing the array size after shifting elements
Fix: Decrease the size variable by one after shifting to reflect the new array length
Mistake: Shifting elements in the wrong direction (right instead of left)
Fix: Shift elements from the deletion index towards the left to fill the gap
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 and keep the order.
The key is to slide all elements after the deleted one left by one position to close the gap.