Only one number in a set of arrays (two numbers) appears once, and the others appear in pairs to find the number

Source: Internet
Author: User
Tags assert

only one number appears once in a set of data. All other numbers appear in pairs. Please find out the number. (using bit operations)> can understand this: If two numbers are equal, the result of their XOR or after is 0.   and 0 is different from any number or is the number itself. (for example, the 00000001^00000001 result is 0.) 00000000^00000001=00000001) then all the elements in a set of numbers are different or, the same number result is 0, and the final result is the number that appears alone. The programming is implemented as follows:
#include <stdio.h>#include<windows.h>intMain () {intarr[]={1,2,3,4,5,1,2,3,4}; intI=0; intj=0;  for(i=1;i<sizeof(arr)/sizeof(arr[0]); i++) {arr[0]=arr[0]^arr[i];//make them whole different or}printf ("The single is:%d\n", arr[0]); System ("Pause");return 0; }

Well, if it's a set of data, two numbers appear once. All other numbers appear in pairs. How to find these two numbers. (using bit operations)

This problem can be done in a regular way, iterating through the array, looking for a different number, and printing it out. But then what to do with the above method, first we analyze: and the same as above the whole array xor, get a result, the value can be seen as the two number to find the difference or derived. For example, a group of numbers 1,2,3,1 1:00000001 1:00000001 2:00000010 3:00000011 xor: 00000001 The overall XOR value is certainly not equal to 1, then the bit is not 0, the bit is located in phase The same data, the bit is the same, the bit is different bits of data, and now divided into two groups to store the two parts, that is, the same data divided into a group, different data divided into different groups. Regardless of how many elements are stored in each group, the same data must be placed in a group anyway. The question then turns into the style of the first question, where a number appears once in each grouping, and the other numbers appear in pairs. The code is implemented as follows:
#include <stdio.h>#include<windows.h>#include<assert.h>voidFind_diff_data (intArr[],intLen)    {assert (arr); ASSERT (Len>2); inti =0; intValue = arr[0];  for(i =0; I < len;i++) {Value^= Arr[i];//Whole XOR or    }    intFarg =1; I=0;  while(I < +)    {        if(Value & (Farg <<= i))//find a bit of 1        {             Break; } I++; } I=0; intData1 =0; intData2 =0;  for(i =0; I < len;i++)    {        if(Arr[i] &Farg) {data1^=Arr[i]; }        Else{data2^=Arr[i]; }} printf ("%d%d", DATA1,DATA2);}intMain () {intArr[] = {1,2,3,4,5,6,1,2,3,4}; intLen =sizeof(arr)/sizeof(arr[0]);    Find_diff_data (Arr,len); System ("Pause"); return 0;}

Only one number in a set of arrays (two numbers) appears once, and the others appear in pairs to find the number

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.