Two digits in an array that appear only once

Source: Internet
Author: User

Title:

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, requirements: time complexity of O (n), Space complexity O (1)

Test Examples:

Input:

8

{2,4,3,6,3,2,5,5}

Output:

4,6

Solution:

Use XOR or solve a problem: A number xor or oneself equals 0, XOR or other number! = 0, if it is a number, then a trip through the array xor the result is what we want, and now is 2 data, then we need to carefully consider how to separate the two numbers:

First: All the numbers are different or, get a result is not equal to 0 of the number, so this result number on bits must have at least one of 1(because that 2 number is different), we start from the right side of the result to select a bit of 1, As the nth bit (this bit is 1, then it means that the first number of this position is 1, the second number is definitely 0 at this position, or vice versa, otherwise it cannot be different or! = 0); Then according to this bit, the array elements are divided into two sub-arrays, the first subarray of each number of the nth bit 1, The nth bit of each number in the second subarray is 0; Since the standard of our grouping is whether one of the numbers is 0 or 1, the number that appears two times is definitely divided into the same array, then the two parts are again different or get two numbers;

Code Implementation

Find integer number rightmost is 1 bit int findFistBitIs1 (int number) {int index = 0;        while ((index & 1) = = 0) && index <= sizeof (int) * 8) {number >>= 1;    + + index; } return index;    Test number from the right indexbit bit is 1int isbit1onindex (int no., int indexbit) {int testnumber = 1 << indexbit; Return (number & Testnumber);}    int main () {freopen ("Input.txt", "R", stdin);    int n;    CIN >> N;    int *array = new Int[n];    for (int i = 0; i < n; ++i) {cin >> array[i];    } int testnumber = 0;    for (int i = 0; i < n; ++i) {testnumber ^= array[i];    } int indexOf1 = FINDFISTBITIS1 (Testnumber);    int ans1,ans2;    ans1 = Ans2 = 0;        for (int i = 0; i < n; ++i) {if (Isbit1onindex (ARRAY[I],INDEXOF1)) {ans1 ^= array[i];        } else {ans2 ^= array[i];    }} cout << ans1 << "<< ans2 << Endl; Delete []arraY;} 


Two digits in an array that appear only once

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.