/*single number*/
/*
input[2,2,1] [4,1,2,1,2]
output:1 4
*/
// i -> 2 j-> 2 2 1 ->2
// i -> 2 j-> 2 2 1 ->2
// i -> 1 j-> 2 2 1 ->1
#if 0
int search_single_number(int * num,const int numsize)
{
int i,j;
for(i = 0;i < numsize;++i)
{
int count = 0;
for(j = 0;j < numsize;++j)
{
if(num[j] == num[i])
count ++;
}
if(count == 1)
return num[i];
}
return;
}
#endif
/*
按位异或
A^A = 0;
A^0 = A;
A^B = B^A;
*/
#if 1
int search_single_number(int * num,const int numsize)
{
int i ;
int n = num[0];
for(i = 1;i < numsize;++i)
{
n = n ^ num[i];
}
return n;
}
#endif
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
//int num[3] = {2,2,1};
int num[5] = {4,1,2,1,2};
int target = search_single_number(num,sizeof(num) / sizeof(num[0]));
printf("target = %d
",target);
return ;
}