First,In an array except for a number , the other numbers appear two times. to use XOR or to solve
#include <iostream>using namespacestd;intMain () {intT; intn,m; while(cin>>t,t) {cin>>N; while(--T) {cin>>m; N=m^N; } printf ("%d\n", N); } return 0;}
Extensions:In an array of integers, except for two digits , the other numbers appear two times.
Http://www.cnblogs.com/youxin/p/3349834.html
data: 1 2 1 3 5 3 4 6 5 6Solution:1, with the above method from S [0] has been with each number of different or, finally get n=6, (2)2. Divide the array into two arrays that contain only one number (2 or 4), and then do so. The original array is divided 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. 3, how to divide? Since these two numbers are not the same, this XOR result contains the characteristics of these two numbers. Take the first step to the n=6, (110) 2 Find the number of 1 in bits as a group. That is:* 1* and 10* two groups, then is divided into: {3,3,2,6,6} and {1,1,5,4,5} two groups. How to find? How do you score after you find it? 4, then how to deal with the group it?
//Test case:int s[10]={1,2,1,3,5,3,4,6,5,6};//function: Find two occurrences of a number in an array//function Parameters: Arr is the source array, Len is the number of elements, and result is used to store the result//return value: NonevoidFindisolatetwo (int*arr,intLenint*result) { //int len = sizeof (arr)/sizeof (arr[0]);result[0] = result[1] =0; intI, all =0, flag =1; for(i =0; i < Len; i++)//all number XOR orAll ^=Arr[i]; while(! (All&flag))//Look for filter bit, (All&flag) not 0 jump outFlag <<=1; for(i =0; i < Len; i++)//using filter bits to differentiate { if(flag&Arr[i]) result[0] ^=Arr[i]; Elseresult[1] ^=Arr[i]; }}
Extension 2:
There are three numbers in the array that appear only once, and the other numbers happen two times to find out the three numbers
Http://www.cppblog.com/flyinghearts/archive/2014/10/16/198695.html
The beauty of programming-----to find the two numbers that have occurred only once in an array of integers.