Skip to content

Instantly share code, notes, and snippets.

@xiren-wang
Created July 31, 2014 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiren-wang/3a0fc1e9f158ec2f8ca9 to your computer and use it in GitHub Desktop.
Save xiren-wang/3a0fc1e9f158ec2f8ca9 to your computer and use it in GitHub Desktop.
single number inside an array
/*Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/
int singleNumber(int A[], int n) {
if (n <= 0)
return -1;
int a0 = A[0];
for(int i=1; i<n; i++)
a0 ^= A[i];
return a0;
}
@xiren-wang
Copy link
Author

Have to first check n <=0, otherwise a0=A[0] may crash for illegal memory access

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment