Algorithm problem: Finding two occurrences of a number in an array of integers

Source: Internet
Author: User
Tags bitwise time 0

Problem: In an array of integers except for two digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. The time complexity is O (n) and the space complexity is O (1).

Analysis: This is a very novel topic about bit arithmetic.

First consider a simple version of this problem: an array of integers in addition to a number, the other numbers appear two times, please write the program to find the only one occurrence of the number.

Where is the breach of this problem? The nature of the array in the topic is that only one integer appears once, and the others appear two times. This makes us think of the nature of the XOR operation: any number that is bitwise XOR or itself equals 0, because the nature of the XOR is the same as 0 different from 1. That is, if you start from beginning to end by bitwise XOR or every integer in the array, the final result is exactly the one that appears once. Then 22 of the same integers in the XOR process equals 0 offset. at the same time 0 the bitwise XOR of any integer is the result of the integer itself .

Based on the above simple solution, go back to the question of the topic. If the original array can be divided into two sub-arrays, each sub-array contains a single occurrence of the number, while the other numbers appear two times. If you can split the original array in this way, then the problem becomes the two arrays in the split to look for the two occurrences of the first number.

So the rest of the question is, according to what conditions to split the score group?

The solution is to do the bitwise XOR of all the integers, because the other pairs of integers that have a bitwise XOR result equal to 0, so that the final XOR result is equivalent to the two occurrences of the integer XOR, so to distinguish between the two integers, you can distinguish between the results of their XOR. Find a bit of 1 in their XOR result, which is the nth bit. According to the operating principle of XOR, then the nth bit of the two numbers must be 11 for 0. The entire array is divided into two groups according to the nth bit, which is 1 or 0. can satisfy each sub-array contains a single occurrence of an integer, the other number 22 pairs appear in different sub-arrays.

Algorithm steps:

(1) The result of an XOR operation is obtained by the bitwise XOR or each integer in the array.

(2) To find the binary form of the result of the XOR operation, the lowest bit of 1 corresponds to the subscript value. Use this bit as a criterion for distinguishing two integers.

(3) According to the criteria of differentiation, the entire array is divided into two sub-arrays, respectively, to find their own XOR operation value, and get two results of the operation is required to two occurrences of the integer.

function declaration:

BOOL Findtwosinglenums (int nums[], int nlength, int &a, int &b); bool IsBit1 (int num, uint indexbit); UINT FindFirst BITIS1 (int num);

Code implementation:

bool findtwosinglenums (int nums[], int nlength, int &a, int &b) {if (nLength    < 2) {return false;    } int resxor = 0;    for (int i = 0; i < nlength; i++) {resxor ^= nums[i];    }//for UINT index = FINDFIRSTBITIS1 (RESXOR);        for (int i = 0; i < nlength; i++) {if (IsBit1 (Nums[i], index)) {a ^= nums[i];        } else {b ^= nums[i];    }}//for}//findtwosinglenums ()//finds the lowest bit of a number with a subscript value of 1, the subscript starts with a zero-based UINT FindFirstBitIs1 (int num) {UINT index = 0;        UINT MAXINDEX = sizeof (num) * 8;        Note the precedence of the operator while ((num & 1) = = 0) && (Index < MAXINDEX)) {num >>= 1;    ++index; }//while return index;}    FINDFIRSTBITIS1 ()//subscript starts at 0, so the number of bits moved to the right is indexbitbool IsBit1 (int num, uint indexbit) {num >>= indexbit; return num & 1;} IsBit1 

PS: Keywords--the same number bitwise XOR or result is 0, and the array is divided into two parts according to the position of the first occurrence of all the elements or 1 (the lowest bit of 1 appears).

Algorithm problem: Finding two occurrences of a number in an array of integers

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.