Java implements numbers that appear only once in an array

All but two numbers in an integer array appear twice. Please write a program to find these two numbers that appear only once.

code

Solution one

It's stupid, sort first. Yes, the same numbers are next to each other, so different numbers must be inconsistent with the numbers before and after. For the first and last elements of the array, they need to be processed separately.

    public static List<Integer> findSignleAppearNumber(int[] array) {
        if (array == null || array.length <= 1) {
            return null;
        }
        // If the length of the array is odd, it is impossible to have two numbers that appear only once
        if (array.length % 2 == 1) {
            return null;
        }
        List<Integer> result = new ArrayList<>(2);
        if (array.length == 2) {
            for (int i : array) {
                result.add(i);
            }
            return result;
        }
        // Sort the array so that two numbers appear next to each other
        Arrays.sort(array);
        // According to the previous judgment conditions, the length of the array is greater than or equal to 4 at this time
        // If the first number and the second number are not equal, the first number is a number that only appears once
        if (array[0] != array[1]) {
            result.add(array[0]);
        }
        // In order to compare with the numbers before and after, the first number and the last number are not compared
        for (int i = 1; i < array.length - 1; i++) {
            // If a number is not equal to the two numbers before and after it, the number appears only once
            if (array[i] != array[i - 1] && array[i] != array[i + 1]) {
                result.add(array[i]);
            }
        }
        // If the traversal is not found, the last number of the array is to be found
        if (result.size() < 2) {
            result.add(array[array.length - 1]);
        }
        return result;
    }


    public static void main(String[] args) {
        int[] array = {1, 2, 2, 3};
        List<Integer> list = findSignleAppearNumber(array);
        for (Integer i : list) {
            System.out.println(i);
        }
    }

Solution two

With the help of XOR, any number XOR itself is equal to 0, only if both are 0 or both are 1, it is 1, if not, it is 0

    public static List<Integer> findSignleAppearNumber2(int[] array) {
        if (array == null || array.length <= 1) {
            return null;
        }
        // If the length of the array is odd, it is impossible to have two numbers that appear only once
        if (array.length % 2 == 1) {
            return null;
        }
        List<Integer> result = new ArrayList<>(2);
        if (array.length == 2) {
            for (int i : array) {
                result.add(i);
            }
            return result;
        }
        // XOR of any number is itself equal to 0, so the result of xor is the XOR of two numbers that occur only once
        int xor = 0;
        for (int i = 0; i < array.length; i++) {
            xor ^= array[i];
        }
        // Find the position where the first bit of the XOR result is 1
        // Use this to split the original array into two subarrays, each containing a number that occurs only once
        // Because the bit is 1, it means that the two numbers are not the same in the bit, that is, one must be 0 in the bit and the other is 1, which is due to the XOR rule
        // So to divide the array in this way, the two numbers must be in different sub-arrays
        int bitIndex = findFirstBitIs1(xor);
        int n1 = 0;
        int n2 = 0;
        for (int i = 0; i < array.length; i++) {
            if (is1OfBitIndex(array[i], bitIndex)) {
                n1 ^= array[i];
            } else {
                n2 ^= array[i];
            }
        }
        result.add(n1);
        result.add(n2);
        return result;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939743&siteId=291194637