Octagram Labs
JavaScriptData StructuresReact
Sign in
← Back to problems

Remove by Index

Immutable Arrayseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 111: The Spread Operator (...)

Implement `removeAt(list, index)` returning a new array without the element at `index`.

Sample tests

Input: removeAt([1,2,3], 1)
Output: [1,3]
Input: removeAt([1], 0)
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • splice removes in place and mutates the original array; prefer filter or slice.
  • Removing by value instead of index gives the wrong result when the list has duplicates.

Learning resources

  • React: Updating Arrays in State
Approach & explanation (try first)

filter((_, i) => i !== index) walks the array and builds a new one containing every element except the one at the target index, without changing the original. An out-of-range index simply keeps all elements.

Loading...
⌘/Ctrl + Enter

Run your code to see results.