I just started on my leetcode exercises and I ran into a bit of a problem. I usually work on the leetcode problems on vsc and then when I got my answer, i just copaste it onto the leetcode answer box.
I’m currently working on Top interview 150’s remove element problem, and weirdly I got different results on them..
here’s my code:
//case 1
let array = [3, 2, 2, 3];
let value = 3;
expected output = [2,2]
//case 2
let arrayOne = [0, 1, 2, 2, 3, 0, 4, 2];
let valueOne = 2;
expected output = [0,1,3,0,4]
const removeElement = (nums, val) => {
let filteredNums = [];
nums.forEach((item) => {
if (item !== val) {
filteredNums.push(item);
}
})
nums = filteredNums;
return nums.length;
};
console.log(removeElement(array, value)); // returns 2, [2,2]
console.log(removeElement(arrayOne, valueOne)); //returns 5 [0,1,3,0,4]
I tried logging my solution and I got the expected output. however when i got to leetcode
the output for case 1 was [3,2]
the output for case 2 was [0,1,2,2,3]
I’m not really sure how this could happen. Was my solution wrong ?
btw, i noticed someone having the exact same problem as me,but it was pointed out that he didn’t modify the result of nums, but I did.
6