The number that appears only once in the array 40

Source: Internet
Author: User

??

Analyze problems

??

Any number xor itself equals 0, traversing the array through this thought, using a result to accumulate XOR or operation, if encountered a certain number two times, must eliminate the XOR operation, in other words, if we start from scratch or array of every number, Then the final result should be the one that only appears once.

??

If you want to be more clear, give me an example.

??

Instance

??

For example int[] array={4,5,5}; We declare a result=0, and then sequentially use result to XOR each number in the array, result XOR 4, 0100, then XOR 5, the result is 0001, then XOR or 5, the result is 0100, That is to find out the 4

??

Problem conversion

??

And the actual problem is that there are two only one occurrence of the number, how to convert it, we need to divide the array into two groups, each group has only one, not to be

??

For example int[] array={2,4,3,6,3,2,5,5}; We declare a result=0 and then sequentially use result to XOR each number in the array, result XOR 2, 0010, then XOR or 4, The result is 0110, then XOR 3, the result is 0101, then XOR or 6, the result is 0011, then XOR or 3, the result is 0000, then XOR or 2, the result is 0010, then XOR or 5, the result is 0111, then XOR or 5, the result is 0010

??

int result=0;

for (int i:array)

Result^=i;

??

What is the use of this result for us, we can according to the second-to-last digit of the result is 1, the array is divided into two groups, respectively, two sub-arrays to be different or, you can get two sub-arrays only one occurrence of the number, here need to declare two result, here we use number 1 and number2 overrides

??

int number1=0,number2=0;

for (int i:array)

{

if (isBit1 (i, index))

Number1^=i;

Else

number2^=i;????????????????

}

System.out.println (NUMBER1);????????

System.out.println (NUMBER2);

??

where isbit1 is to determine whether the index bit of a number is 1

??

Private Boolean isBit1 (int number,int index)

{

Number=number >> Index;

Return (number & 1) ==0;

}

??

??

In addition here we know that the result of the score group is the penultimate 1, and then in practice we need a function to tell us the countdown number is 1,

??

private int findFirstBitIs1 (int number)

{

int indexbit=0;

while ((number & 1) ==0)

{

Number=number >> 1;

++indexbit;

}

return indexbit;

}

??

The number that appears only once in the array 40

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.