Java - a number that appears only once in an array

topic link

Niuke online oj question - the number that only appears once in the array

topic description

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

problem solving ideas

The simplest solution to this problem is: use a hash table to record the number of occurrences of all numbers, and then directly return the two numbers that appear only once

If there is only one number in the array that appears only once

This problem can be solved by XOR

XOR operation: Each bit of the binary number corresponding to the two numbers is 1 if they are different, and 0 if they are the same.
Therefore, the following properties can be obtained:

  1. XOR of 0 and any number is that number
  2. XORing two identical numbers yields 0

Therefore, we can create a variable tmp, let tmp = 0, and then XOR all elements in the array with tmp, because the XOR of two identical numbers is 0, so all numbers that appear twice in the array are Eliminated, and the element that appears once and 0 XOR is still itself, and finally return directly to tmp

There are two numbers in the array that appear only once

If we still do this question according to the idea that there is only one number that only appears once, then what is the tmp obtained by XORing all elements?

If tmp = 3, binary is 00000000000000000000000000000011

Then according to the nature of XOR, if the binary bits are the same, XOR is 0, and if the binary bits are different, XOR is 1, because other numbers that appear twice are eliminated, so it can be obtained that the two numbers that only appear once are the most The one digit from the right and the second to last digit from the right are both different

We only need one bit to be different. For example, the rightmost bit is different. We can divide all the elements in the array into two groups. The binary rightmost bit of the first group of elements is 0, and the second group of elements The rightmost side of the binary is 1

In this case, two numbers that only appear once are divided into two different groups, we only need to XOR each group to get the two numbers that only appear once in the array

full code

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

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130493745