[leetcode] In an array where each number appears K times, find the number that appears only once (the idea is convinced)

The topics are as follows:

Given an array of integers, every element appears three times except for one. Find that single one.
Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Let's discuss, twice and n times

Occurs twice:

Idea 1: According to the characteristics of XOR operation : XOR two identical numbers, the result is 0, XOR from beginning to end, such as 5, 5, 4, 3, 3, its binary

Represented as 101, 101, 100, 11, 11, the result of XOR is: 0, 100, 111, 100, and the final result is 100

Code:

public static int singleNumber(int[] A) {
    int num = 0;
    for(int i=0;i<A.length;i++){
        num^=A[i];
    }
    return num;
}

Idea 2: To traverse the array, consider whether the numbers in the middle are the same before and after, and deal with the first number separately

Code:

import java.util. *;

public class Solution {
    public int singleNumber(int[] A) {
        int i=0;
        Arrays.sort(A);
        for(i=0;i<A.length-1;i++){
            if(i==0){
                if(A[i]==A[i+1]){continue;}
            }else{
                if(A[i]==A[i+1]||A[i]==A[i-1]){
                continue;
                 }
            }
            break;
        }
        return A[i];
    }
}
 What to do when there are three or more occurrences to find that single value? It seems that XOR cannot be used, but considering that the input is an int array, 32 bits can be used to express the elements of the input array.
Assuming that there is no single number in the input, then each number in the input has a repeated number, that is, for each i in the 32 bits, after all the inputs are added up, the i-th bit must be 3 multiples of .
Now that the single number is added, do the same for each of the 32 bits, that is, add up all the inputs bit by bit, and look at the remainder when the sum of the i-th bit is divided by 3, the remainder It is the value of single numer in the i-th position. In this way, the value of the i-th position of the single number is obtained. This is equivalent to an emulated binary, then just convert the emulated binary to a decimal output.

In addition, this method can be extended. If there is a bunch of inputs, one of the numbers appears once, and the remaining numbers appear K times. All such problems can be solved using this method.

full code

public class SingleNum {
	public static void main(String[] args) {
		SingleNum s=new SingleNum();
		int[] arr= {2,2,2,5,2,3,4,5,4,5,4,5,4};
		System.out.println(s.singleNumber(arr,4));
	}
	public static int singleNumber(int A[],int k) {  
		int n=A.length;
        int[] count=new int[32];  
        int result=0;  
        for(int i=0;i<32;i++){  
            for(int j=0;j<n;j++){  
            	count[i]+=((A[j]>>i)&1);
            	//First add up the i-th digit of the input number, and here with 1, and get one digit
                count[i]=count[i]%k;
                //Then find the remainder of dividing them by k.  
            }  
            result|=(count[i]<<i);//Convert the result of binary representation to the result of decimal representation  
        }  
        return result;  
    }  
}

Guess you like

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