A number that appears only once in an array of "point of the sword"

Source: Internet
Author: User

Original blog, reproduced please indicate the source!

The address of the cattle network

Code GitHub Address

Other face question index address

# topics

# ideas

The nature of an XOR operation: any number and the result of its own XOR or operation is 0.

    • An element of an array appears once

If you start from beginning to end, or every number in an array, the result is a number that appears only once.

For example : {4,5,5}, the binary form of the first element in the array is 0100, the second element has a binary form of 0101, and the third element has a binary form of 0101. In turn, the three elements are XOR, and the result of 0100 and 0101 XOR is 0001,0001 and 0101 xor The result is 0100,0100 decimal digit 4.

    • Two elements appear once in the array

The array is first split into two sub-arrays, each of which contains a solitary element, and then the solitary elements are found separately from the sub-array. Array Splitting method : From start to finish or array of each number, the result of the binary representation of at least one is 1, set the result of the first bit of 1 is the nth bit, according to the nth bit is not 1, the array of numbers into two sub-arrays, the first subarray of each number of the nth bit is 1, The nth bit of each number in the second subarray is 0.

For example : {2,4,3,6,3,2,2,5}, in which the element in the array is an XOR result of 0010, the first bit in the result is a third bit, and the array is split into two sub-arrays {2,3,6,3,2} and {4,5,5}, depending on whether the third bit is 1.

# code

1 2 classSolution {3  Public:4     //data is an array, NUM1 is the first solitary number, num2 is the second solitary number5     voidFindnumsappearonce (vector<int> Data,int*NUM1,int*NUM2)6{7         //Special input check8         intLength = Data.size ();9         if(Length < 2)Ten             return; One  A         //XOR each element of the original array or -         intResultexclusiveor = 0; -          for(inti = 0; i < length; ++i) theResultexclusiveor ^= Data[i]; -  -         //Find the first 1 bit in the XOR result -         unsigned intINDEXOF1 = FINDFIRSTBITIS1 (Resultexclusiveor); +  -         //Look for numbers that appear only once num1,num2 +*NUM1 = *num2 = 0; A          for(intj = 0; J < length; J + +) at             if(IsBit1 (Data[j], INDEXOF1)) -*num1 ^= Data[j]; -             Else -*num2 ^= Data[j]; -} - Private: in  -     //Find binary number num The first digit of 1, such as 0010, the first 1 is 2.  to     unsigned intFINDFIRSTBITIS1 (intNUM) { +         unsigned intindexbit = 0; -         //Only one byte is judged the          while(num & 1) = = 0 && (Indexbit < 8 *sizeof(unsigned int))){ *num = num >> 1; $indexbit++;Panax Notoginseng} -         returnIndexbit; the} +  A     //Determine if the indexbit bit is 1 the     BOOLIsBit1 (intNumunsigned intIndexbit) { +num = num >> indexbit; -         return(Num & 1); $} $};

A number that appears only once in an array of "point of the sword"

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.