The XOR operator has the property that 'a' XOR 'a' will always be 0, that is, they are canceled, so if you know that your list has only one duplicate and that the range says x to y, 601 - 607 in your case It is possible to store the xor of all elements from x to y in a variable, and then xor this variable with all the elements that you have in your array. Since there will be only one element that will be duplicated, it will not be undone due to the xor operation, and that will be your answer.
void main() { int a[8]={601,602,603,604,605,605,606,607}; int k,i,j=601; for(i=602;i<=607;i++) { j=j^i; } for(k=0;k<8;k++) { j=j^a[k]; } printf("%d",j); }
This code will return 605 as desired!
Ashwyn
source share