Numbers that appear only once in the JZ40 array

Title description

Except for two numbers in an integer array, the other numbers appear twice. Please write a program to find these two numbers that only appear once.

//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution {
    
    
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
    
    
        if (array.length != 0) {
    
    
            int x = 0;
            int count = 1;
            for (int i : array) {
    
    
                x ^= i;
            }
            while ((x & 1) != 1 ){
    
    
                x = x >> 1;
                count++;
            }

            for (int i : array) {
    
    
                if (((i >> (count-1)) & 1) == 1) {
    
    
                    num1[0] ^= i;
                }else {
    
    
                    num2[0] ^= i;
                }
            }
        }

    }

}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41620020/article/details/108569623