删除Java数组中的重复元素。
以下是必需的程序。
示例
import java.util.Arrays;
public class Tester {
public static int[] removeDuplicateElements(int arr[]){
int n = arr.length;
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
return temp;
}
public static void main (String[] args) {
int arr[] = {10,70,30,90,20,20,30,40,70,50};
//对数组排序
Arrays.sort(arr);
int[] result = removeDuplicateElements(arr);
//打印数组元素
for (int i=0; i<result.length; i++) {
if(result[i] != 0){
System.out.print(result[i]+" ");
}
}
}
}输出结果
10 20 30 40 50 70 90
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短