双指针
左右指针,一开始左指针指向索引为0的数字,右指针指向索引为1的数字,如果左右指针指向的值相等,那么右指针右移,当不相等的时候,左指针替换右指针,右指针+1
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicate {
public static void main(String[] args) {
int[] nums = {0, 0, 0, 0, 1, 1, 1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 9, 10, 12, 13};
RemoveDuplicate removeDuplicate = new RemoveDuplicate();
for (Integer i : removeDuplicate.removeDuplicate(nums)) {
System.out.print(i + " ");
}
}
public List<Integer> removeDuplicate(int[] nums) {
List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList());
int left = 0;
int right = 1;
while (right <= nums.length - 1) {
while (nums[left] == nums[right]) {
list.remove(nums[right]);
right++;
}
left = right;
right += 1;
}
return list;
}
}