A list contains a set of numbers, one number presents once and other numbers present even no. of times. Find out the number that occurs once in the list.

+3 votes

A list contains a set of numbers, one number presents once and other numbers present even no. of times. Find out the number that occurs once in the list.

+1 vote

Following C program gives the expected.

```
#include<stdio.h>
int main()
{
int numList[] = { 12, 12, 14, 14, 15, 15, 16 };
int diffNum = 0;
int i;
int totalNum = sizeof(numList)/sizeof(numList[0]);
for (i=0; i < totalNum; i++)
{
diffNum ^= numList[i];
}
printf("Different Number = [%d]", diffNum);
return 0;
}
```

This is the best answer.

0 votes

Just wanted to add little bit complexity in what Harshita had mentioned. This might help you.

int main()

{

int a[] = {1,3,3,1,3,4,4,6,1,6,1};

int n = sizeof(a)/sizeof(a[0]);

int i = 0, j = 0;

```
for(i = 0; i < n; i++){
for(j = i+1; j < n; j++){
if(a[i] == a[j]){
a[j] = -a[j];
break;
}
}
if(i != n-1){
printf("i %d\n",i);
if(a[i] != a[j]){
if(a[i] == -(a[j])){
continue;
}else if(a[i] < 0){
continue;
}else{
printf("Unique Number [%d]\n",a[i]);
break;
}
}
}else{
printf("Unique Number [%d]\n",a[i]);
break;
}
}
return 0;
```

}

...