I am currently facing an issue with my code as it is returning a length of 0 instead of the expected result of 5 after excluding duplicates from the array. I have a feeling that there might be something missing in my code, but I just can't seem to figure out what it is. Could someone please take a quick look and let me know what needs to be corrected? I would really appreciate it if the solution could use my existing code rather than starting from scratch. Thank you!
Question: Given a sorted array nums, remove the duplicates in-place such that each element appears only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
const numbers = [0,0,1,1,1,2,2,3,3,4]
const removeDuplicates = (nums) => {
nums.sort()
const newArr = []
//Alternatively, count the number of unique elements
for(let i = 0; i < nums.length; i++){
for(let j = 0; j < nums.length; j++){
if(!nums[i] === nums[j]){
newArr.push(nums[i]);
}
}
}
console.log(newArr)
return newArr.length
};