[12 of the Classical Vernacular algorithm series] only two numbers appear in the array once (Baidu interview question)

Source: Internet
Author: User

Weibo http://weibo.com/morewindowsis available. Welcome to the Forum.

This article series address: http://blog.csdn.net/MoreWindows/article/category/859207

First, let's look at the question requirements:

In an array, only two numbers appear once, and other numbers appear twice. You need to identify the two numbers as soon as possible.

Consider the simplified version of this question-except for a number in the array that appears only once, other numbers become correct and need to be located as soon as possible. This question has been answered in "bit operation interesting applications" in the previous "bit operation basic article bit operation comprehensive summary. Based on the characteristics of the exclusive or operation, you can find this number directly or once.

Now there are two numbers in the array that appear only once. They can only get the exclusive or result of the two numbers at a time, but the two numbers cannot be obtained from this result. Therefore, let's analyze the key points of the "XOR" solution in the simplified version, which is also quite obvious --An array can only contain one number once..

In the question, the numbers A and B appear only once. If a and B can be separated into two arrays, it is clearly in line with the key points of the "XOR" solution. Therefore, the key point of this question is to separate a and B into two arrays.Because A and B are definitely not equal, one of them must be different in binary. You can separate A and B to Group A and Group B based on whether the position is 0 or 1.The other numbers in this array belong to either Group A or Group B. Then, perform the "XOR" solution for Group A and Group B respectively to obtain group A and group B. However, to determine which of the following is a and B is different, you only need to know the result of A or B. This result indicates a in binary, B is different in this field.

For example, int A [] = {1, 1, 3, 5, 2, 2}

Returns an exclusive or whole array of 3 ^ 5, that is, 0x0011 ^ 0x0101 = 0x0110

For 0x0110, 1st bits (from low to high, starting from 0) are 1. Therefore, the entire array is divided into two groups based on whether the 1st-bit value is 0 or 1.

A [0] = 1 0x0001 first group

A [1] = 1 0x0001 first group

A [2] = 3 0x0011 second group

A [3] = 5 0x0101 first group

A [4] = 2 0x0010 second group

A [5] = 2 0x0010 second group

The first group has {1, 1, 5}, and the second group has {3, 2, 3}. Obviously, the two groups can get 5 and 3 by performing the "XOR" solution respectively.

After some analysis, I believe the code is not difficult to write. The complete source code is provided below:

// Baidu interview question // in the array, all the numbers except two appear. Ask for the two numbers as quickly as possible // By morewindows (http://blog.csdn.net/MoreWindows) # include <stdio. h> void findtwonotrepeatnumberinarray (int * a, int N, int * pn1, int * pn2) {int I, j, temp; // calculate the variance or result temp = 0; for (I = 0; I <n; I ++) temp ^ = A [I]; // find the first 1-bit for (j = 0; j <sizeof (INT) * 8; j ++) if (temp> J) & 1) = 1) break; // The J-th digit is 1, indicating that the two numbers are different on the J-th digit. // You can group the two numbers to * pn1 = 0, * pn2 = 0; for (I = 0; I <n; I ++) if (A [I]> J) & 1) = 0) * pn1 ^ = A [I]; else * pn2 ^ = A [I];} void printfarray (int A [], int N) {for (INT I = 0; I <n; I ++) printf ("% d", a [I]); putchar ('\ n');} int main () {printf ("the twelve arrays of the vernacular classical algorithm series do not repeat numbers (Baidu interview) \ n"); printf ("-- by morewindows (http://blog.csdn.net/MoreWindows) -- \ n "); const int maxn = 10; // int A [maxn] = {1, 2, 7, 5,100,100, 6, 1, 2, 5 }; int A [maxn] = {1, 2, 3, 4, 1, 2, 3, 4, 0, 5}; printf ("array: \ n "); printfarray (A, maxn); int nnotrepeatnumber1, nnotrepeatnumber2; random (A, maxn, & nnotrepeatnumber1, & nnotrepeatnumber2); printf ("two non-repeated numbers: % d \ n ", nnotrepeatnumber1, nnotrepeatnumber2); Return 0 ;}

The running result is as follows:

 

Baidu interviews generally have multiple algorithm questions during the algorithm process. I will sort out several more questions next time. You are welcome to continue reading these questions.

 

Address of the Classical Vernacular algorithm series:

Http://blog.csdn.net/MoreWindows/article/category/859207

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8214003

Welcome to Weibo: http://weibo.com/MoreWindows

 

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.