Bird
0
0
DSA Cprogramming

Array Deletion at Beginning in DSA C

Choose your learning style9 modes available
Mental Model
Deleting the first element means removing it and shifting all other elements left by one position.
Analogy: Imagine a line of people waiting. If the first person leaves, everyone else steps forward to fill 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 beginning
Goal: Remove the first element and shift remaining elements left
Step 1: Remove element at index 0 (value 1)
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Why: We identify the element to delete at the start
Step 2: Shift element at index 1 (value 2) to index 0
Index: 0   1   2   3   4
Array: [2]->[2]->[3]->[4]->[5]
Why: Shift left to fill the empty spot
Step 3: Shift element at index 2 (value 3) to index 1
Index: 0   1   2   3   4
Array: [2]->[3]->[3]->[4]->[5]
Why: Continue shifting elements left
Step 4: Shift element at index 3 (value 4) to index 2
Index: 0   1   2   3   4
Array: [2]->[3]->[4]->[4]->[5]
Why: Continue shifting elements left
Step 5: Shift element at index 4 (value 5) to index 3
Index: 0   1   2   3   4
Array: [2]->[3]->[4]->[5]->[5]
Why: Continue shifting elements left
Step 6: Reduce array size by 1 (ignore last duplicate)
Index: 0   1   2   3
Array: [2]->[3]->[4]->[5]
Why: Last element is now duplicate and removed logically
Result:
Index: 0   1   2   3
Array: [2]->[3]->[4]->[5]
Annotated Code
DSA C
#include <stdio.h>

void deleteAtBeginning(int arr[], int *size) {
    if (*size == 0) {
        printf("Array is empty, nothing to delete.\n");
        return;
    }
    // Shift elements left to overwrite first element
    for (int i = 1; i < *size; i++) {
        arr[i - 1] = arr[i];
    }
    (*size)--; // Reduce size by 1
}

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;

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

    deleteAtBeginning(arr, &size);

    printf("After deleting at beginning:\n");
    printArray(arr, size);

    return 0;
}
for (int i = 1; i < *size; i++) { arr[i - 1] = arr[i]; }
Shift all elements left by one to overwrite the first element
(*size)--;
Decrease the array size by one after deletion
OutputSuccess
Original array: 1 -> 2 -> 3 -> 4 -> 5 -> null After deleting at beginning: 2 -> 3 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because we shift all n-1 elements one position left
Space: O(1) because we do the operation in place without extra space
vs Alternative: Compared to linked list deletion at beginning which is O(1), array deletion requires shifting elements making it O(n)
Edge Cases
empty array
Prints message and does nothing
DSA C
if (*size == 0) {
    printf("Array is empty, nothing to delete.\n");
    return;
}
array with one element
After deletion, size becomes zero and array is empty
DSA C
(*size)--;
When to Use This Pattern
When you need to remove the first element from an array and maintain order, use shifting left to fill the gap.
Common Mistakes
Mistake: Not reducing the size after shifting, causing stale data at the end
Fix: Decrease the size variable by one after shifting elements
Mistake: Starting the shift loop from index 0 instead of 1, overwriting data incorrectly
Fix: Start shifting from index 1 to size-1, moving each element to index i-1
Summary
Deletes the first element of an array by shifting all other elements left.
Use when you want to remove the front element but keep the order of remaining elements.
The key insight is that arrays need shifting to fill the gap left by deletion at the start.