Evaluate the number that appears once in the array

Source: Internet
Author: User

Evaluate the number that appears once in the array
Only one number appears in an array once, and the other numbers appear twice. How can we find the number that appears once? For example, if array a [11] = {1, 2, 3, 3, 4, 5, 5, 5, 6}, 1 is displayed at a time, which can be obtained through an exclusive or algorithm.

The Code is as follows:

 

Int onediffent (int a [], int n) {int temp = 0; for (int I = 0; I
 
  

 

Supplement: Any number is different from zero or equal to any number. Any number is different from itself or 0;

If the problem is that there are two numbers in an array and the other numbers appear twice, how can we find these two numbers?

Algorithm idea: first, the group of numbers in the array is exclusive OR, and the result is stored in temp. Then, the location where the delimiter in temp appears is calculated, and then it is connected to all the numbers in the array, in this way, the array is divided into two arrays, and the problem is converted into an array with only one number. The Code is as follows:

Int twodiffent (int a [], int n) {int temp, count, I, j, k; temp = a [0]; j = k = 0; int B [n], c [n]; for (int I = 1; I
   
    
> 1; count * = 2;} // divide the two numbers into two arrays. The problem is to convert them into an array with only one number. for (I = 0; I
    
     
In the algorithm group, an enthusiastic netizen gives us some advice to get the current optimal solution. It has been optimized for at least a century compared with algorithm 2. The Code is as follows:
     

 

void solve(int num[],int n){        int x = 0;        for(int i = 0; i < n; ++i)        {            x = x ^ num[i];        }        int smallestOne = x & -x;        int a = 0;        int b = 0;        for(int i = 0; i < n; ++i)        {            if((num[i] & smallestOne) != 0)                a = a ^ num[i];            else                b = b ^ num[i];        }       printf("%d %d\n",a,b);}

 

In this algorithm, x &-x calculates the first percentile, and the negative number is represented by a complement in the computer, for example, 4 &-4 = 4, 1000 &-= 8 indicates that in number 4, the binary bit 1 contains 2 ^ 2, the decimal 1000 binary 1 digit is displayed when the weight is 2 ^ 3. arrays in algorithm 2 are completely unnecessary. The space complexity is O (1), and the time complexity is O (n );

If you have a better method, you may wish to discuss it here.


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.