📖 Scenario: You have a list of daily temperatures recorded over a week. Sometimes, a wrong temperature is recorded in the middle of the list. You want to remove that wrong temperature from the list.
🎯 Goal: Build a Go program that removes the middle temperature from the list and prints the updated list.
📋 What You'll Learn
Create an array of 7 integers representing temperatures.
Calculate the middle index of the array.
Remove the element at the middle index.
Print the updated array after removal.
💡 Why This Matters
🌍 Real World
Removing incorrect or unwanted data points from a list of measurements is common in data cleaning.
💼 Career
Understanding how to manipulate arrays and slices is essential for software developers working with data processing.
Progress0 / 4 steps
1
Create the temperature array
Create an array called temps with these exact integer values: 23, 25, 22, 30, 24, 26, 28
DSA Go
Hint
Use Go array syntax with exact values inside curly braces.
2
Calculate the middle index
Add a variable called mid that stores the middle index of the temps array. Use the length of temps divided by 2.
DSA Go
Hint
Use len(temps) to get the length and divide by 2.
3
Remove the middle element
Create a slice called updatedTemps that contains all elements of temps except the one at index mid. Use slicing and append to combine the parts before and after mid.
DSA Go
Hint
Use append to join slices before and after the middle index.
4
Print the updated array
Print the updatedTemps slice using fmt.Println to show the array after removing the middle element.
DSA Go
Hint
Use fmt.Println(updatedTemps) to print the slice.
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
Step 1: Understand array deletion
Deleting an element removes it and shifts later elements left.
Step 2: Effect on array size
Removing one element reduces the array length by one.
Final Answer:
The middle element is removed and the array size decreases by one. -> Option B
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
Step 1: Identify correct slicing syntax
In Go, to delete an element at index i, use append(arr[:i], arr[i+1:]...).
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:]...).
Final Answer:
arr = append(arr[:len(arr)/2], arr[len(arr)/2+1:]...) -> Option C
Quick Check:
Use append with slices excluding middle [OK]
Hint: Use append(arr[:i], arr[i+1:]...) to delete index i [OK]
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
Step 1: Check slice ranges in append
The code uses arr[mid:] which includes the middle element again, not deleting it.
Step 2: Correct slice to exclude middle element
To delete middle element at mid, use arr[mid+1:] to skip it.
Final Answer:
The second slice should start from mid+1, not mid. -> Option A
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
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].
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].
Step 3: Verify final array
Final array is [5 10 15 100 25 30 35].
Final Answer:
[5 10 15 100 25 30 35] -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.