Algorithms and Data Structures · Junior · Question 75 of 75
How does arr.splice(i, 1) in the middle of an array affect performance?
- 1It's O(log n) because arrays are trees internally
- 2It's O(1): it only marks the slot as empty
- ✓Correct answer: It's O(n): elements after
ishift one position left
Explanation
Removing from the middle keeps the array dense, so everything after the index moves. Deleting many elements one by one in a loop is O(n²); filter into a new array is O(n).