去除数组中的重复元素

方法一

import java.util.Arrays;

public class Mytest1 {
    public static void main(String[] args) {
        int[] a = {2, 1, 3, 2, 4, 5, 4, 6, 7, 4, 6, 7, 4};
        //先排序,使数组有序
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] == a[i + 1]) {
                continue;
            } else {
            //如果不相等,添加a[i]
                sb.append(a[i]).append(",");
            }
        }
        //这里最后要加上数组最后一个元素
        sb.append(a[a.length - 1]);
        String s = sb.toString();
        String[] split = s.split(",");
        int[] arr = new int[split.length];
        int index = 0;
        for (int i = 0; i < split.length; i++) {
            arr[index] = Integer.parseInt(split[i]);
            index++;
        }
        System.out.println(Arrays.toString(arr));

    }
}

方法二

import java.util.Arrays;

public class Mtest2 {
    public static void main(String[] args) {
        int[]a={2,2,2,3,4,6,7,8,5,4,6,8,3,5};
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < a.length; i++) {
            //如果sb中不存在就添加到sb里面
            if(sb.indexOf(String.valueOf(a[i])) == -1){
                sb.append(a[i]).append(",");
            }
        }
        String s = sb.toString();
        String[] split = s.split(",");
        int[] arr = new int[split.length];
        int index = 0;
        for (int i = 0; i < split.length; i++) {
            arr[index] = Integer.parseInt(split[i]);
            index++;
        }
        System.out.println(Arrays.toString(arr));
    }
}

方法三

import java.util.Arrays;

public class Mytest3 {
    public static void main(String[] args) {
        int[]a={2,3,3,3,4,4,5,5,5,6,6,6,3,3,8,8,9,9};
        Arrays.sort(a);
        int[] arr = new int[a.length];
        arr[0] = a[0];
        int index = 1;
        for (int i = 0; i < a.length-1; i++) {
            if(a[i] != a[i+1]){
                arr[index] = a[i+1];
                index++;
            }
        }
        int[] ints = Arrays.copyOf(arr, index);
        System.out.println(Arrays.toString(ints));

    }
}

方法四
采用选择排序的思想,通过两层for循环,刚开始用第一个数字和后面的所有数字进行比较,如果有相等的,那么就跳过去了,i加一,直到循环结束,但是这样当到时第一个和第二个数字不相等的时候,最后一个数字就无法放进新数组里面,所以最后判断原来数组中到时第一个和第二个数字是否相等,来判断是否给新数组中放入。

import java.util.Arrays;

public class Mytest4 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3,5,4,6,7,1,1,2,3,4,7,7,23,23,2};
        int[] arr = new int[a.length];
        int index = 0;
        for (int i = 0; i < a.length - 1; i++) {
            arr[index] = a[i];
            boolean flag = true;
            for (int j = i + 1; j < a.length; j++) {
                if (arr[index] == a[j]) {
                    flag = false;
                }
            }
            if (flag) {
                // arr[index] = a[i];//这一步可以可无,因为在前面 已经执行了  arr[index] = a[i],这里的if语句只是用来给索引增加值
                index++;
            }

        }
        arr[index] = a[a.length-1];
        int[] copyArr = Arrays.copyOf(arr, index + 1);
        System.out.println(Arrays.toString(copyArr));
    }
}

代码最后再将原数组的最后一个赋给新数组的最后一个即可,为了更好的理解,读者可以举出1,2,3和1,2,3,4自己再草稿纸上把流程走一遍,相信就会知道为什么了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值