public static int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
} else if (nums.length == 1) {
return 1;
} else {
int number = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[number] != nums[i]) {
number ++;
nums[number] = nums[i];
}
}
return ++number;
}
}
public static void main(String[] args) {
int[] nums = {1,1,2,2,3,3,4,4,5,5};
int i = removeDuplicates(nums);
System.out.println(i);
}
数组去重,并输出数组长度
最新推荐文章于 2022-04-04 11:39:38 发布