public class DuplicatedNumberHandler {
public static void main(String[] orgs){
int[] arr = {1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,6};
DuplicatedNumberHandler handler = new DuplicatedNumberHandler();
int[] result = handler.deleteDuplicatedNumbers(arr);
for(int i : result){
System.out.println(i);
}
}
public int[] deleteDuplicatedNumbers(int[] arr){
if(arr == null){
return null;
}
int[] ret = new int[arr.length];
int length = arr.length;
ret[0] = arr[0];
int current = ret[0];
int count = 1;
for(int pivot =1;pivot <= length - 1;pivot++){
if(current != arr[pivot]){
current = arr[pivot];
ret[count++] = current;
}
}
return ret;
}
public boolean isSorted(int[] arr,boolean asc){
boolean ret = true;
int length = arr.length;
for(int i = 0,j=i ; i < length - 1; i++,j++){
if(asc){
if(arr[i] > arr[j]){
ret = false;
break;
}
}else{
if(arr[i] < arr[j]){
ret = false;
break;
}
}
}
return ret;
}
}
删除已排序数组中的重复数
最新推荐文章于 2023-02-14 21:04:54 发布