Two numbers appear only once in the array, and two numbers appear once in the array

Source: Internet
Author: User

Two numbers appear only once in the array, and two numbers appear once in the array
Question:

In an integer array, all numbers except two appear twice. Write a program to find the numbers that appear only once. The time complexity is O (n), and the space complexity is O (1)

 

Test example:

Input:

8

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

Output:

4,6

 

Solution:

Use exclusive or solve the problem: a number exclusive or equal to 0, exclusive or another number! = 0. If it is a number, the result after traversing the array exclusive or is what we want; but now it is two pieces of data, we need to carefully consider how to separate the two numbers:

First, perform the XOR operation on all numbers to obtain a number whose result is not equal to 0, therefore, the result number must have at least one digit as 1 (because the two digits are different) in the binary bit. We select a digit as 1 from the right of the result, it is recorded as the nth digit (this digit is 1, which means that the first digit is 1, and the second digit is 0, or vice versa. Otherwise, it cannot be different or! = 0); then, based on the characteristics of this bit, the array elements are divided into two sub-arrays, the nth bit of each number in the first sub-array is 1, in the second sub-array, the nth digits of each number are all 0. because the standard of our grouping is whether a digit is 0 or 1, the two numbers are certainly divided into the same array, and then the two parts are differentiated again or two numbers are obtained;

 

Code Implementation

// Search for the int findFistBitIs1 (int number) {int index = 0; while (index & 1) = 0) & index <= sizeof (int) * 8) {number >>=1; ++ index;} return index ;} // test whether the right-side indexBit of number is 1int isBit1OnIndex (int number, 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 ;}


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.