top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

A list contains a set of numbers, one number presents once and other numbers present even no. of times.

+3 votes
104 views

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.

posted Oct 26, 2015 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+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;
    }
answer Oct 27, 2015 by Harshita
This is the best answer.
0 votes

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

include <stdio.h>

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;

}

answer Nov 24, 2015 by Ganesh Annigeri
...