(Classic XOR technique) A number that appears only once in an array (two methods)

Source: Internet
Author: User
Tags arrays
A number that appears only once in an array

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. Method One: The classical XOR technique uses the different or the operation (is considered the standard idea bar)

Ideas:
This topic examines the characteristics of an XOR operation: that is, two identical numbers are different or the result is 0.
This topic uses two different or operation characteristics:
(1) For the first time, an XOR operation is used to obtain the results of two distinct or different occurrences.
(2) because two only appear once the number certainly is different, namely their difference or the result must not be 0, must have a bit to have 1. There is no 1 on this bit, we can according to this bit on whether there is 1, the entire array is divided into two parts, some of this bit must have 1, the other part of this bit must not be 1, and then separately for each part of the difference or, because the two parts of the division has such a feature: The other number appears two times, only one Therefore, we can use the XOR operation, respectively, to get the two parts only one occurrence of the number.


Another way of thinking explains:
The idea is to use XOR, but it is different from finding a separate number in a paired number that the lowest bit of 1, which requires the use of an XOR result, divides the numbers in the array into two categories, one with flag bitwise and 0, and the other as not 0, so that the two numbers can be found separately or once. It's very ingenious. a more detailed thinking process:

Consider the process:
Let's first consider a simple version of the problem: in an array, the numbers appear two times in addition to a number. Please write the program to find the only one occurrence of the number.
where is the breach of this topic? Why do you want to emphasize that there is a number appearing once, the other appears two times. We think of the nature of the XOR operation: any one number XOR itself equals 0. That is, if we vary from start to finish or every number in the array, the final result is exactly the one that appears only once, because those numbers that appear two times are all offset by XOR.
with the solution to the simple problem above, we go back to the original question. If the original array can be divided into two sub-arrays. In each sub-array, there is a number that appears only once, and the other numbers appear two times. If you can split the original array in this way, the previous method is to find out the two occurrences of the number only once.
We're still different from beginning to end, or every number in an array, then the result is an XOR result of two occurrences of a single number. Because the other numbers appear two times, they all cancel out in the XOR. Since these two numbers are definitely different, the XOR result is definitely not 0, meaning that at least one of the binary representations of the result number is 1. We find the position of the first 1 bit in the result number, which is recorded as the nth bit. Now we divide the numbers in the original array into two sub-arrays with the nth bit not 1, and the nth bit for each number in the first Subarray is 1, and the nth bit for each number of the second subarray is 0.
Now we have divided the original array into two sub-arrays, each containing a single occurrence of the number, while the other numbers appear two times. So far, all the problems we have solved. */

Class Solution {public
:
    void Findnumsappearonce (vector<int> data,int* num1,int *num2) {
        if ( Data.size () < 2) return;
        int myxor = 0;
        int flag = 1;
        for (int i = 0; i < data.size (); + + i)
            myxor ^= data[i];
        while (Myxor & flag) = = 0) flag <<= 1;
        *NUM1 = Myxor;
        *num2 = Myxor;
        for (int i = 0; i < data.size (), + + i) {
            if ((flag & data[i) = = 0) *num2 ^= data[i];
            else *num1 ^= data[i];}}
;
Method Two:

This is little white my approach, for reference only
Ideas:
sort the elements in data first, then look for them, with only two occurrences and one occurrence in data .
Should compare understood, is no different or so tall on the

class Solution {public:void findnumsappearonce (vector<int> data, int* num1, int *num2 ) {//Sort array for (int i = 0; I<data.size ()-1; i++) {for (int j = 0; J<data.size ()-1-i; j+
        +) {if (data[j]>data[j + 1]) swap (Data[j], data[j + 1]); }} for (int i = 0; i < data.size (); i++) cout << i << "th" << data[i] << Endl

    ; Then iterate over the ordered vector, looking for the two number of occurrences of int count = 0;//Two number of the flag bit for (int i = 0; I<data.size ()-1; i++) {if (count

        = = 2) break;
        if (data[i] = = data[i + 1]) {i++;
            } else {count++;
            if (count = = 1) *num1 = Data[i];
        if (count = = 2) *num2 = Data[i];
   }} if (count = = 1)//Last count or 1, then the last one must be only one occurrence of the number *num2 = Data[data.size ()-1]; }
};

There are other good ways to add that the
has not been continued .....

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.