I am facing a unique challenge with my Javascript function that counts the number of sorted numbers in an array. Strangely, it seems to work perfectly fine for positive numbers, but when negative numbers are involved, the function treats them as if they were positive. Can anyone provide some insight into why this might be happening?
countUniqueValues = (a) => {
if(a.length === 0){return 0;}
let i = 0;
let j = 1;
while(a[j]){
if(a[i] === a[j]){
j++;
}
else if(a[i] !== a[j]){
i++;
a[i] = a[j];
}
}
return i+1;
}
console.log(countUniqueValues([-2,-1,-1,0,1])); // returns 2, should actually return 4