Bird
Raised Fist0
DSA Cprogramming

Array Deletion at Beginning in DSA C

Choose your learning style10 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.

Practice

(1/5)
1. What happens to the elements of an array when you delete the first element (at the beginning)?
easy
A. All elements shift one position to the right, and the last element is removed.
B. All elements shift one position to the left, and the first element is removed.
C. The array size increases by one.
D. The array remains unchanged.

Solution

  1. Step 1: Understand deletion at beginning

    Deleting the first element means removing the element at index 0.
  2. Step 2: Effect on remaining elements

    All other elements move one position left to fill the gap.
  3. Final Answer:

    All elements shift one position to the left, and the first element is removed. -> Option B
  4. Quick Check:

    Deletion at start shifts elements left [OK]
Hint: Deleting start shifts all elements left by one [OK]
Common Mistakes:
  • Thinking elements shift right instead of left
  • Assuming array size increases
  • Believing array stays unchanged
2. Which of the following C code snippets correctly deletes the first element of an integer array arr of size n by shifting elements left?
easy
A. for(int i = 0; i < n; i++) { arr[i] = arr[i]; } n--;
B. for(int i = n - 1; i > 0; i--) { arr[i] = arr[i - 1]; } n++;
C. for(int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--;
D. for(int i = 1; i < n; i++) { arr[i] = arr[i - 1]; } n--;

Solution

  1. Step 1: Identify correct loop for shifting left

    The loop must start from index 0 and assign arr[i] = arr[i+1] up to n-2.
  2. Step 2: Check array size update

    After shifting, size decreases by 1 (n--).
  3. Final Answer:

    for(int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } n--; -> Option C
  4. Quick Check:

    Shift left from 0 to n-2, then reduce size [OK]
Hint: Shift elements left from 0 to n-2, then decrease size [OK]
Common Mistakes:
  • Looping backwards instead of forwards
  • Increasing size instead of decreasing
  • Incorrect loop bounds causing overwrite
3. What is the output array after deleting the first element from int arr[] = {10, 20, 30, 40, 50}; using the correct deletion method?
medium
A. [50, 40, 30, 20]
B. [10, 20, 30, 40]
C. [30, 40, 50]
D. [20, 30, 40, 50]

Solution

  1. Step 1: Remove first element 10

    Deleting first element removes 10 from the array.
  2. Step 2: Shift remaining elements left

    Elements 20, 30, 40, 50 shift left to fill index 0 to 3.
  3. Final Answer:

    [20, 30, 40, 50] -> Option D
  4. Quick Check:

    First element removed, others shifted left [OK]
Hint: After deletion, array starts from second element [OK]
Common Mistakes:
  • Leaving original first element in array
  • Removing last element instead
  • Reversing array order
4. Consider this code snippet for deleting the first element of an array:
for(int i = 1; i < n; i++) { arr[i-1] = arr[i]; } n--;

What is the error in this code?
medium
A. The loop correctly shifts elements; no error present.
B. The loop should start from i = 0, not 1.
C. The array size should be increased, not decreased.
D. The assignment should be arr[i] = arr[i-1], not arr[i-1] = arr[i].

Solution

  1. Step 1: Analyze loop start and assignments

    The loop starts at i=1 and assigns arr[i-1] = arr[i], shifting elements left correctly.
  2. Step 2: Check array size update

    Decreasing n by 1 after shifting is correct to reflect deletion.
  3. Final Answer:

    The loop correctly shifts elements; no error present. -> Option A
  4. Quick Check:

    Loop from 1 with arr[i-1]=arr[i] shifts left [OK]
Hint: Loop from 1 with arr[i-1]=arr[i] shifts left correctly [OK]
Common Mistakes:
  • Thinking loop must start at 0 for shifting left
  • Confusing assignment direction
  • Incorrectly changing array size
5. You have an array int arr[] = {5, 10, 15, 20, 25, 30}; and size n = 6. You delete the first element twice using the correct deletion method. What is the resulting array and size?
hard
A. Array: [15, 20, 25, 30], Size: 4
B. Array: [10, 15, 20, 25, 30], Size: 5
C. Array: [5, 10, 15, 20], Size: 4
D. Array: [20, 25, 30], Size: 3

Solution

  1. Step 1: First deletion removes 5

    After first deletion, array becomes [10, 15, 20, 25, 30], size 5.
  2. Step 2: Second deletion removes 10

    After second deletion, array becomes [15, 20, 25, 30], size 4.
  3. Final Answer:

    Array: [15, 20, 25, 30], Size: 4 -> Option A
  4. Quick Check:

    Two deletions remove first two elements, size reduces by 2 [OK]
Hint: Each deletion removes first element and reduces size by one [OK]
Common Mistakes:
  • Not updating size after each deletion
  • Deleting wrong elements
  • Confusing array indexes after shifts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard