Find two occurrences of the number in the array "Microsoft interview 100 question 61st"

Source: Internet
Author: User

Title Requirements:

An integer array except for two digital machines, the other numbers appear two times.

Please write the program to find the two only occurrences of the number. Requires time complexity O (N). Space complexity O (1).

Reference: Sword refers to the 40th question of offer.

Topic Analysis:

Known:

1. Two the same number XOR result is 0, i.e. A^a = 0.

2. Two different numbers xor the result of a binary in which one is 1, then the two digits of the binary corresponding bit one is 0, one is 1. as 3^2 = 1, for the lowest bit binary, the lowest bit binary of 3 is the lowest bit bits 0, then the lowest bit binary of the result 1 is definitely 1.

Assuming that the original array only appears once in the two numbers of A and B, then, the original array of all the elements of the result C, that is, the result of a^b (because the other XOR is 0), that is a^b = C;

according to the known condition 2, we find the second binary in the binary in the bit 1, the original array is divided into two parts x and Y (A and B respectively in X and Y, and X and y respectively are only one number appears once, other numbers appear two times ), The result of all the elements in X will certainly be the result of an XOR of all the elements in the a,y.

Code implementation:

  

#include <iostream>using namespacestd;Const intN =6;BOOLFindnumsappearonce (intData[],intLenint&AMP;NUM1,int&num2);intMainvoid){    intData[n] = {4,2,6,2,4,5};//int Data[n] = {4,2,6,2,4,6};    intnum1,num2; if(Findnumsappearonce (data,n,num1,num2)) {cout<<"NUM1 ="<< NUM1 <<", num2 ="<< num2 <<Endl; }    Elsecout<<"not satisfied with the situation"<<Endl; return 0;} unsignedintFINDFIRSTBITIS1 (intnum) {    intindex =0;  while((num&1)==0&& (index<8*sizeof(int)) {num>>=1; ++index; }    returnindex;}BOOLIsBit1 (intnum,unsignedintindex) {num>>=index; return(num&1);}BOOLFindnumsappearonce (intData[],intLenint&AMP;NUM1,int&num2) {    if(Data==null | | len<2|| ((len&0x01)==1))        return false; intresult =0;  for(intI=0; i<len;i++) Result^=Data[i]; if(result==0)        return false; unsignedintindex =FindFirstBitIs1 (Result); NUM1= Num2 =0;  for(intj =0; j<len;j++)    {        if(IsBit1 (data[j],index)) Num1^=Data[j]; Elsenum2^=Data[j]; }    return true;}

  

Find two occurrences of the number in the array "Microsoft interview 100 question 61st"

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.