删除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 教师闺蜜祝福语简短