删除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 佛教生日祝福语简短大全