What's up guys, I was just messing arround LeetCode, and different ways to solving this problem (I know the Set way and the for of with array.includes + push, and the filter, but all of them create a new array), and with this method I'm getting this output, can someone explain me why?
P.S. You can't create a new array, just modify the first one.
let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
var removeDuplicates = (nums) => {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === nums[i + 1]) {
nums.splice(i, i + 1);
}
}
};
removeDuplicates(nums);
console.log(nums);
// [0, 1, 3, 4]