DEV Community

Discussion on: Removing an Element in an Array In-Place

Collapse
 
salyadav profile image
Saloni Yadav

The problem with splice is, it has an O(n) time complexity in worst case. Since this question doesnot demand the exact array and bothers not about what exists after the returned length of the array...
The following could be a better approach that splicing:

  1. swap elements if you encounter val (push it to the back of the array)
  2. just copy the non "val" values to the array index< return length