Bird
Raised Fist0
DSA C#programming

Array Deletion at Middle Index in DSA C#

Choose your learning style10 modes available
Mental Model
To delete an element in 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 in 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]
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)
Index:  0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Why: We identify the element to remove
Step 2: Shift element at index 3 (value 4) to index 2
Index:  0   1   2   3   4
Array: [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
Index:  0   1   2   3   4
Array: [1]->[2]->[4]->[5]->[5]
Why: Continue shifting elements left
Step 4: Reduce array size by 1 (ignore last duplicate)
Index:  0   1   2   3
Array: [1]->[2]->[4]->[5]
Why: Remove last duplicate element logically by reducing size
Result:
Index:  0   1   2   3
Array: [1]->[2]->[4]->[5]
Annotated Code
DSA C#
using System;

class ArrayDeletion
{
    // Deletes element at given index by shifting elements left
    public static int[] DeleteAtIndex(int[] arr, int index)
    {
        int n = arr.Length;
        if (index < 0 || index >= n)
        {
            Console.WriteLine("Index out of bounds");
            return arr;
        }
        for (int i = index; i < n - 1; i++)
        {
            arr[i] = arr[i + 1]; // shift element left
        }
        // Create new array with size one less
        int[] newArr = new int[n - 1];
        for (int i = 0; i < n - 1; i++)
        {
            newArr[i] = arr[i]; // copy shifted elements
        }
        return newArr;
    }

    static void Main()
    {
        int[] array = {1, 2, 3, 4, 5};
        int deleteIndex = 2;
        int[] result = DeleteAtIndex(array, deleteIndex);
        Console.WriteLine(string.Join(" -> ", result) + " -> null");
    }
}
if (index < 0 || index >= n)
check if index is valid to avoid errors
for (int i = index; i < n - 1; i++)
shift elements left starting from deletion index
arr[i] = arr[i + 1];
move next element to current position to fill gap
int[] newArr = new int[n - 1];
create new smaller array to hold updated elements
for (int i = 0; i < n - 1; i++)
copy shifted elements to new array
return newArr;
return updated array without deleted element
OutputSuccess
1 -> 2 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because we shift elements after the deleted index once
Space: O(n) because we create a new array with one less element
vs Alternative: Compared to linked lists where deletion is O(1) if node is known, arrays require shifting elements which costs O(n)
Edge Cases
delete at index 0 (first element)
shifts all elements left, removes first element
DSA C#
if (index < 0 || index >= n)
delete at last index
no shifting needed, just reduce size by one
DSA C#
for (int i = index; i < n - 1; i++)
delete with invalid index (negative or too large)
prints error and returns original array unchanged
DSA C#
if (index < 0 || index >= n)
When to Use This Pattern
When you need to remove an element from an array and keep order, use the shifting pattern to fill the gap left by the deleted element.
Common Mistakes
Mistake: Not shifting elements after the deleted index, leaving a duplicate or gap
Fix: Shift all elements after the deleted index one position left to fill the gap
Mistake: Not reducing the array size after shifting, causing duplicate last element
Fix: Create a new array with size one less and copy shifted elements
Mistake: Deleting with an invalid index causing runtime error
Fix: Check index bounds before deletion and handle invalid cases gracefully
Summary
Deletes an element at a given index by shifting subsequent elements left and resizing the array.
Use when you want to remove an element from an array and maintain the order of remaining elements.
The key insight is that deletion in arrays requires shifting elements to close the gap left by the removed element.

Practice

(1/5)
1. What happens when you delete the middle element from an array in C#?
easy
A. The element is removed and all elements after it shift left by one position.
B. The array size automatically decreases by one without shifting elements.
C. The middle element is replaced by zero but array size stays the same.
D. The array throws an error because arrays have fixed size.

Solution

  1. Step 1: Understand array behavior on deletion

    In C#, arrays have fixed size, so to delete an element, we shift elements after it left to overwrite it.
  2. Step 2: Effect on array elements

    After shifting, the last element is duplicated or ignored logically, but the middle element is effectively removed from the sequence.
  3. Final Answer:

    The element is removed and all elements after it shift left by one position. -> Option A
  4. Quick Check:

    Deletion shifts elements left [OK]
Hint: Deleting means shifting elements left to fill the gap [OK]
Common Mistakes:
  • Thinking array size changes automatically
  • Assuming deletion replaces element with zero
  • Believing arrays throw error on deletion
2. Which of the following C# code snippets correctly deletes the middle element from an integer array arr of odd length?
easy
A. int mid = arr.Length / 2; arr[mid] = 0;
B. int mid = arr.Length / 2; for(int i = mid; i < arr.Length - 1; i++) arr[i] = arr[i + 1];
C. int mid = arr.Length / 2; arr.RemoveAt(mid);
D. int mid = arr.Length / 2; Array.Delete(arr, mid);

Solution

  1. Step 1: Identify valid deletion method in arrays

    Arrays in C# do not have RemoveAt or Delete methods; we must shift elements manually.
  2. Step 2: Check code correctness

    int mid = arr.Length / 2; for(int i = mid; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; calculates middle index and shifts elements left from middle to end-1, correctly deleting the middle element.
  3. Final Answer:

    int mid = arr.Length / 2; for(int i = mid; i < arr.Length - 1; i++) arr[i] = arr[i + 1]; -> Option B
  4. Quick Check:

    Manual shift after mid index [OK]
Hint: Use loop to shift elements left from middle index [OK]
Common Mistakes:
  • Using non-existent array methods like RemoveAt
  • Just setting middle element to zero without shifting
  • Trying to delete without shifting elements
3. What is the output of the following C# code?
int[] arr = {10, 20, 30, 40, 50};
int mid = arr.Length / 2;
for(int i = mid; i < arr.Length - 1; i++)
    arr[i] = arr[i + 1];
for(int i = 0; i < arr.Length - 1; i++)
    Console.Write(arr[i] + " ");
medium
A. 10 20 30 50
B. 10 20 30 40 50
C. 10 20 40 50
D. 10 20 40 50 50

Solution

  1. Step 1: Calculate middle index and shift elements

    Array length is 5, mid = 2. Loop shifts arr[2] = arr[3] (40), arr[3] = arr[4] (50).
  2. Step 2: Print elements except last

    Loop prints arr[0]=10, arr[1]=20, arr[2]=40, arr[3]=50. Last element ignored.
  3. Final Answer:

    10 20 40 50 -> Option C
  4. Quick Check:

    Shifted middle element out = 10 20 40 50 [OK]
Hint: Shift elements left, print all but last [OK]
Common Mistakes:
  • Printing full array including last duplicate
  • Not shifting elements correctly
  • Confusing middle index calculation
4. Find the error in this code that tries to delete the middle element from an array arr of length 6:
int mid = arr.Length / 2;
for(int i = mid; i <= arr.Length - 1; i++)
    arr[i] = arr[i + 1];
medium
A. Array elements are not shifted correctly.
B. Middle index calculation is wrong for even length arrays.
C. No error; code works fine.
D. Loop condition causes index out of range error.

Solution

  1. Step 1: Analyze loop range and array indexing

    Loop runs from mid to arr.Length - 1 inclusive, accessing arr[i + 1]. For i = arr.Length - 1, arr[i + 1] is out of bounds.
  2. Step 2: Identify cause of error

    Accessing arr[arr.Length] causes IndexOutOfRangeException.
  3. Final Answer:

    Loop condition causes index out of range error. -> Option D
  4. Quick Check:

    Loop index out of bounds [OK]
Hint: Loop must stop before last index to avoid out of range [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Ignoring array bounds when accessing i+1
  • Misunderstanding middle index for even length
5. You have an array arr = {5, 10, 15, 20, 25, 30, 35}. Write the steps to delete the middle element and print the resulting array with correct size handling.
hard
A. Create a list from arr; remove element at mid; convert back to array; print.
B. Calculate mid = arr.Length / 2; shift elements left from mid; print first arr.Length - 1 elements.
C. Use Array.RemoveAt(mid); print arr.
D. Set arr[mid] = 0; print arr as is.

Solution

  1. Step 1: Convert array to list for flexible size

    Arrays have fixed size, so convert to List<int> to remove element easily.
  2. Step 2: Remove middle element and convert back

    Remove element at mid index, then convert list back to array for printing.
  3. Step 3: Print resulting array

    Print elements of new array which has one less element.
  4. Final Answer:

    Create a list from arr; remove element at mid; convert back to array; print. -> Option A
  5. Quick Check:

    Use List for deletion, then convert back [OK]
Hint: Use List to delete middle element easily, then convert back [OK]
Common Mistakes:
  • Trying to resize arrays directly
  • Only shifting elements without resizing
  • Using non-existent Array.RemoveAt method
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