Algorithm 14: find two numbers that appear only once in the array.

Source: Internet
Author: User

[Topic] All numbers except two in an integer array appear twice. Find the numbers that appear only once in the array. Time complexity O (N) and space complexity O (1) are required ).

 

[Thinking path] First, let's consider a simple case: if there is only one number in the array and only appears once, the other numbers appear twice, how should we find this number? What do we mean when the number appears twice? We can easily think of an exclusive or operation, because any number is 0 and the result of its own exclusive or operation. Once we know this, it is easy to know that we want all numbers to be exclusive or, the result is a number that appears only once, because all the other numbers are different or 0.

With the simple question above as an introduction, we can easily think that if we successfully divide the array of the original question into two subarrays, each sub-array exactly contains a number that appears only once, so we can solve it directly based on the above ideas. How can we divide these two subarrays?

We still perform the exclusive or operation on all the numbers in the original array. It is easy to know that the exclusive or result is actually the exclusive or result of two numbers that only appear once, because the two numbers are not equal, therefore, the result of an exclusive or is certainly not 0. We can find the first digit in the exclusive or result as 1, which indicates that there must be a number 1 on this digit, the value of another number is 0. Based on this result, we can divide all numbers with this bit as 1 into one group, and divide the digits with this bit as 0 into another group, in this way, the original problem is converted into two simple sub-problems. Then, we can obtain two numbers that appear only once when the subproblem is completely different or different. According to this idea, we can get the followingCode:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 // Judge whether the bitindex of a number is 1
6 Bool Isbit1 ( Int Number, unsigned Int Bitindex)
7 {
8 Number = number> bitindex;
9 Return (Number & 1 );
10 }
11
12 // Locate the first BIT in num that is 1
13 Unsigned Int Findfirstbitof1 ( Int Num)
14 {
15 Int Bitindex = 0 ;
16 While (Bitindex < 32 & (Num & 1 ) = 0 ))
17 {
18 Num = num> 1 ;
19 Bitindex ++;
20 }
21
22 // Another loop structure of the function, which is completely equivalent
23 /* For (bitindex = 0; bitindex <32; ++ bitindex, num = num> 1)
24 {
25 If (Num & 1) = 1)
26 Break;
27 }
28 */
29 Return Bitindex;
30 }
31
32 Void Findnumbersappearonce ( Int Data [], unsigned Int Length, Int & Num1, Int & Num2)
33 {
34 // Invalid Input
35 If (Data = NULL | length < 2 )
36 Return ;
37
38 // Obtain num1 variance or num2
39 Int Resultofbitor = 0 ;
40 For ( Int I = 0 ; I <length; ++ I)
41 {
42 Resultofbitor ^ = data [I];
43 }
44
45 // Locate the first digit in the result of num1 ^ num2
46 Unsigned Int Index = findfirstbitof1 (resultofbitor );
47
48 Num1 = 0 ;
49 Num2 = 0 ;
50
51 // Whether the index value is 1 is divided into two groups
52 // Returns all numbers in each group.
53 For ( Int J = 0 ; J <length; ++ J)
54 {
55 If (Isbit1 (data [J], index ))
56 Num1 ^ = data [J];
57 Else
58 Num2 ^ = data [J];
59 }
60
61
62 }
63
64 Int Main ()
65 {
66 Cout < " Please enter your arraylength: " <Endl;
67 Int Arraylength = 0 ;
68 Cin> arraylength;
69
70 Int * Array = New Int [Arraylength];
71 Cout < " Please enter the numbers in your array: " <Endl;
72 For ( Int K = 0 ; K <arraylength; ++ K)
73 {
74 Cin> array [k];
75 }
76
77 Int Result1 = 0 ;
78 Int Result2 = 0 ;
79 Findnumbersappearonce (array, arraylength, result1, result2 );
80
81 Cout < " The numbers appear once in your array are: " <Endl;
82 Cout <result1 < " \ T " <Result2 <Endl;
83
84 Return 0 ;
85 }

The test and running results are as follows:

 

References:

He Haitao blog: http://zhedahht.blog.163.com/blog/static/2541117420071128950682/

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.

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.