Bird
Raised Fist0
DSA Goprogramming

Array Deletion at Middle Index in DSA Go

Choose your learning style10 modes available
Mental Model
To remove an element from the middle of an array, shift all elements after it one step 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 books after it one spot 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: Identify element at index 2 (value 3) to delete
Index: 0   1   2   3   4
Array: [1]->[2]->[3]->[4]->[5]
Why: We need to remove the element at index 2
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 left by deleted element 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 to close the gap
Step 4: Reduce array size by 1 to remove duplicate last element
Index: 0   1   2   3
Array: [1]->[2]->[4]->[5]
Why: Remove the last duplicate element after shifting
Result:
Index: 0   1   2   3
Array: [1]->[2]->[4]->[5]
Annotated Code
DSA Go
package main

import "fmt"

func deleteAtMiddle(arr []int, index int) []int {
    if index < 0 || index >= len(arr) {
        return arr // index out of range, return original
    }
    for i := index; i < len(arr)-1; i++ {
        arr[i] = arr[i+1] // shift element left to fill gap
    }
    return arr[:len(arr)-1] // reduce slice size by 1
}

func main() {
    arr := []int{1, 2, 3, 4, 5}
    fmt.Println("Original array:", arr)
    arr = deleteAtMiddle(arr, 2)
    fmt.Println("After deletion at index 2:", arr)
}
if index < 0 || index >= len(arr) {
check if index is valid to avoid errors
for i := index; i < len(arr)-1; i++ {
shift elements left starting from deletion index
arr[i] = arr[i+1]
move next element to current position to fill gap
return arr[:len(arr)-1]
reduce array size by one to remove duplicate last element
OutputSuccess
Original array: [1 2 3 4 5] After deletion at index 2: [1 2 4 5]
Complexity Analysis
Time: O(n) because we may need to shift up to n-1 elements after the deleted index
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) extra space), this method is more space efficient
Edge Cases
delete at index 0 (first element)
All elements shift left, first element removed
DSA Go
if index < 0 || index >= len(arr) {
delete at last index
No shifting needed, just reduce size by one
DSA Go
if index < 0 || index >= len(arr) {
delete with invalid index (negative or too large)
Array remains unchanged
DSA Go
if index < 0 || index >= len(arr) {
When to Use This Pattern
When you need to remove an element from the middle of an array and keep order, use shifting left to fill the gap.
Common Mistakes
Mistake: Not reducing the array size after shifting, leaving duplicate last element
Fix: Return the slice excluding the last element with arr[:len(arr)-1]
Mistake: Shifting elements starting from 0 instead of the deletion index
Fix: Start shifting from the deletion index to avoid overwriting unrelated elements
Summary
Deletes an element at a given middle index by shifting subsequent elements left and reducing array size.
Use when you want to remove an element from an array without losing order.
The key is to shift elements left starting from the deletion point and then shorten the array.

Practice

(1/5)
1. What happens when you delete the middle element from an array with an odd number of elements?
easy
A. The array size stays the same but the middle element becomes zero.
B. The middle element is removed and the array size decreases by one.
C. The array is reversed after deletion.
D. The first element is removed instead of the middle.

Solution

  1. Step 1: Understand array deletion

    Deleting an element removes it and shifts later elements left.
  2. Step 2: Effect on array size

    Removing one element reduces the array length by one.
  3. Final Answer:

    The middle element is removed and the array size decreases by one. -> Option B
  4. Quick Check:

    Delete middle element = size - 1 [OK]
Hint: Deleting removes element and shrinks array size [OK]
Common Mistakes:
  • Thinking array size stays the same after deletion
  • Assuming deletion replaces element with zero
  • Confusing middle deletion with first element deletion
2. Which Go code snippet correctly deletes the middle element from an array named arr with odd length?
easy
A. arr = arr[len(arr)/2+1:]
B. arr = append(arr[1:], arr[:len(arr)-1]...)
C. arr = append(arr[:len(arr)/2], arr[len(arr)/2+1:]...)
D. arr = arr[:len(arr)/2] + arr[len(arr)/2+1:]

Solution

  1. Step 1: Identify correct slicing syntax

    In Go, to delete an element at index i, use append(arr[:i], arr[i+1:]...).
  2. Step 2: Apply to middle index

    Middle index is len(arr)/2 for odd length, so arr = append(arr[:len(arr)/2], arr[len(arr)/2+1:]...).
  3. Final Answer:

    arr = append(arr[:len(arr)/2], arr[len(arr)/2+1:]...) -> Option C
  4. Quick Check:

    Use append with slices excluding middle [OK]
Hint: Use append(arr[:i], arr[i+1:]...) to delete index i [OK]
Common Mistakes:
  • Using + operator to join slices (invalid in Go)
  • Not using ... to expand second slice in append
  • Deleting wrong index or slice ranges
3. What is the output of this Go code?
arr := []int{10, 20, 30, 40, 50}
mid := len(arr) / 2
arr = append(arr[:mid], arr[mid+1:]...)
fmt.Println(arr)
medium
A. [10 20 40 50]
B. [10 20 30 40]
C. [20 30 40 50]
D. [10 30 40 50]

Solution

  1. Step 1: Calculate middle index

    len(arr) = 5, so mid = 5 / 2 = 2 (integer division).
  2. Step 2: Delete element at index 2

    Element at index 2 is 30. After deletion, arr = [10, 20] + [40, 50] = [10 20 40 50].
  3. Final Answer:

    [10 20 40 50] -> Option A
  4. Quick Check:

    Removed 30 at index 2 [OK]
Hint: Middle index = length/2; remove that element [OK]
Common Mistakes:
  • Using float division instead of integer
  • Deleting wrong index element
  • Not expanding slice with ... in append
4. Find the error in this Go code that tries to delete the middle element from arr:
arr := []int{1, 2, 3, 4, 5}
mid := len(arr) / 2
arr = append(arr[:mid], arr[mid:]...)
fmt.Println(arr)
medium
A. The second slice should start from mid+1, not mid.
B. The middle index calculation is wrong.
C. append cannot be used with slices.
D. The array length is even, so no middle element.

Solution

  1. Step 1: Check slice ranges in append

    The code uses arr[mid:] which includes the middle element again, not deleting it.
  2. Step 2: Correct slice to exclude middle element

    To delete middle element at mid, use arr[mid+1:] to skip it.
  3. Final Answer:

    The second slice should start from mid+1, not mid. -> Option A
  4. Quick Check:

    Exclude middle element by slicing from mid+1 [OK]
Hint: Second slice must start at mid+1 to delete middle [OK]
Common Mistakes:
  • Using arr[mid:] includes middle element again
  • Miscalculating middle index for odd length
  • Thinking append can't join slices
5. Given an array arr := []int{5, 10, 15, 20, 25, 30, 35}, write the Go code to delete the middle element and then insert 100 at the same middle index. What will be the final array?
hard
A. [5 10 15 20 100 25 30 35]
B. [5 10 15 20 25 30 35 100]
C. [5 10 15 20 25 30 35]
D. [5 10 15 100 25 30 35]

Solution

  1. Step 1: Delete middle element

    Length is 7, middle index = 7/2 = 3. Delete element at index 3 (value 20): arr = append(arr[:3], arr[4:]...) = [5 10 15 25 30 35].
  2. Step 2: Insert 100 at middle index

    Insert 100 at index 3: arr = append(arr[:3], append([]int{100}, arr[3:]...)...) = [5 10 15 100 25 30 35].
  3. Step 3: Verify final array

    Final array is [5 10 15 100 25 30 35].
  4. Final Answer:

    [5 10 15 100 25 30 35] -> Option D
  5. Quick Check:

    Delete middle, insert 100 at same index [OK]
Hint: Delete middle then insert at same index using append [OK]
Common Mistakes:
  • Not updating array after deletion before insertion
  • Inserting 100 at wrong index
  • Appending 100 at end instead of middle
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