Data Structures · Visualization
Remove at Index
Array
Removing from an array deletes the value at a chosen position and closes the gap it leaves behind, so the array stays contiguous with no holes. Time: O(n) worst case (removing near the start shifts almost everything). Space: O(1) extra space.
Big-O: Random access O(1); insert/delete in the middle O(n).
Remove at Index
ArrayRemoving from an array deletes the value at a chosen position and closes the gap it leaves behind, so the array stays contiguous with no holes. This is the mirror image of inserting: instead of shifting elements right to make room, you shift elements left to fill the empty spot.
How Remove at Index Works
- Locate the element at the target index to remove.
- Shift every element after that index one position to the left.
- Continue shifting until the last element has moved.
- Reduce the tracked length of the array by one.
Remove at Index Worked Example
Input: Remove the element at index 1 from [10, 20, 30, 40]
- Element at index 1 is 20 — it will be removed.
- Shift 30 left from index 2 to index 1: [10, 30, 30, 40]
- Shift 40 left from index 3 to index 2: [10, 30, 40, 40]
- Drop the last duplicate slot: [10, 30, 40]
Output: [10, 30, 40]
Press ▶ Run to watch it step by step.
def remove_at(arr, index):
removed = arr[index]
for i in range(index, len(arr) - 1):
arr[i] = arr[i + 1] # shift left
arr.pop() # drop the tail
return removed
a = [10, 20, 30, 40]
remove_at(a, 1) # removes 2
# example: remove at index at index 1 on [10, 20, 30, 40]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Remove at Index Time & Space Complexity
When to Use Remove at Index
- Deleting a completed task from a task list while keeping the rest in order
- Removing a player who has left a game's turn order
- Cleaning invalid entries out of a dataset before processing it
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.