这道理主要还是使用快慢指针,和 27 题一样 注意这里是有序链表,所以如果是数组,必须先给数组排序,然后才能这样做
function removeDuplicates(arr) {
if (arr.length === 0) {
return 0;
}
let slow = 0,
fast = 0;
while (fast < arr.length) {
if (arr[fast] !== arr[slow]) {
slow++;
arr[slow] = arr[fast];
}
console.log(fast, '--- ');
fast++;
}
arr.length = slow;
console.log(arr);
return slow + 1;
}
const a = [1, 1, 2, 0, 2, 0, 3];
const res = removeDuplicates(a);