找出数组中没有重复值的两个值

public static void main(String[] args) {
        int[] arr = {15,1,15,16,1,15,16,13};
        ArrayList<Integer> arr1 = new ArrayList();
        ArrayList<Integer> arr2 = new ArrayList();
        int result = 0;
        for (int i = 0; i < arr.length; i++) {
            result = result ^ arr[i];
        }
        System.out.println(result);

        //分两个数组
        for (int i = 0; i < arr.length; i++) {
            if (isIntNumberNBitONEInBinary(arr[i],2)){
                arr1.add(arr[i]);
            }else{
                arr2.add(arr[i]);
            }
        }
        for (int i = 0; i < arr1.size(); i++) {
            System.out.print(arr1.get(i) + " ");
        }
        System.out.println("");
        System.out.println("--------");
        for (int i = 0; i < arr2.size(); i++) {
            System.out.print(arr2.get(i) + " ");
        }
        System.out.println("");
        System.out.println("--------");
        result = 0;
        for (int i = 0; i < arr1.size(); i++) {
            result = result ^ arr1.get(i);
        }
        System.out.println(result);
        result = 0;
        for (int i = 0; i < arr2.size(); i++) {
            result = result ^ arr2.get(i);
        }
        System.out.println(result);
//        SpringApplication.run(PrintApplication.class, args);
    }

    private static boolean isIntNumberNBitONEInBinary(int number,int nbit){
        boolean result = false;
        if((number%(Math.pow(2, nbit)))/(Math.pow(2, nbit-1)) >= 1.0){
            result = true;
        }
        return result;
    }

一个数组中两个值是单独存在的,其余的值都是重复的两个值。

posted @ 2019-01-17 10:39  忆殇314  阅读(522)  评论(0编辑  收藏  举报