Find two numbers that appear only once in the array

Source: Internet
Author: User

 

From: http://blog.csdn.net/ewanyou/archive/2011/05/25/6445209.aspx

Http://www.cnblogs.com/aLittleBitCool/archive/2011/04/14/2015720.html

Problem description: except two numbers in an array, the remaining numbers appear twice (or even ). Write a program to find the numbers that appear only once. The time complexity is O (n) and the space complexity is O (1 ).

Problem Analysis: This is a novel interview question about bit operations. First, consider a simple version of this problem: Except for a number in an array, the remaining numbers appear twice. Please write a program to find the number that appears once.

Where is the breakthrough? Why does the question emphasize that one number appears only once and the others appear twice? We think of the nature of an exclusive or operation. Any number exclusive or itself is 0. That is to say, if we change from start to end or every number in the array, the final result is exactly the number that appears only once. Because the two numbers are offset in the exclusive or operation.

With the solution to the above simple problem, we will return to the original problem. If you can divide an array into two sub-arrays, each sub-array contains a number that appears only once, and the remaining number appears twice. If the array can be split in this way, it is easy to find two numbers that appear only once in the original array according to the previous method.

We still change the number from start to end or each number in the array, and the final result is the difference or value of two numbers that only appear once, the other two numbers are offset in the exclusive or operation. Because these two numbers must be different, the ultimate variance or result must not be 0. That is to say, one of the binary values of an exclusive or result must be 1. We locate the position of the first digit not 1 in the XOR or result number and mark it as N. We divide the original array into two subarrays based on whether the nth digit is 1. Each number in the first sub-array is 1, and the N-digit in the second sub-array is 0. Now, we have divided the original array into two subarrays. Both sub-arrays contain a number that appears only once, and the remaining number appears twice. So far, the original problem has been solved.

 

 

Algorithm:

 

# Include <iostream> <br/> using namespace STD; <br/> int findfirstone (INT value); <br/> bool testbit (INT value, int POS ); <br/> int findnums (INT date [], int length, Int & num1, Int & num2) {<br/> If (length <2) {return-1 ;} <br/> int ansxor = 0; <br/> for (INT I = 0; I <length; I ++) {<br/> ansxor ^ = date [I]; // returns or <br/>}< br/> int Pos = findfirstone (ansxor ); <br/> num1 = num2 = 0; <br/> for (INT I = 0; I <length; I ++) {<br/> If (testb It (date [I], POS) <br/> num1 ^ = date [I]; <br/> else <br/> num2 ^ = date [I]; <br/>}< br/> return 0; <br/>}< br/> int findfirstone (INT value) {// take the first position in the binary value as 1 <br/> int Pos = 1; <br/> while (Value & 1 )! = 1) {<br/> value = value> 1; <br/> POS ++; <br/>}< br/> return Pos; <br/>}< br/> bool testbit (INT value, int POS) {// test whether a position is 1 <br/> return (value> POS) & 1); <br/>}< br/> int main (void) {<br/> int date [10] = {1, 2, 3, 4, 5, 6, 4, 3, 2, 1 }; <br/> int ans1, ans2; <br/> If (findnums (date, 10, ans1, ans2) = 0) <br/> cout <ans1 <"" <ans2 <Endl; <br/> else <br/> cout <"error" <Endl; <br/> return 0; <br/>} 

 

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.