Skip to content

Instantly share code, notes, and snippets.

@feuyeux
Last active July 24, 2019 00:01
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 feuyeux/58b72ac111b47a9a6e78f52bcba18e18 to your computer and use it in GitHub Desktop.
Save feuyeux/58b72ac111b47a9a6e78f52bcba18e18 to your computer and use it in GitHub Desktop.
exclusive_or_self-inverse
// Using exclusive or self-inverse to find the repeated number in an array
// Self-inverse: A ⊕ A = 0
// A XOR B XOR B = A XOR 0 = A
int[] a1 = {1, 2, 3, 3};
int[] a2 = {1, 2, 2, 3};
int x1 = a1[0] ^ a1[1] ^ a1[2] ^ a1[3] ^ 1 ^ 2 ^ 3;
int x2 = a2[0] ^ a2[1] ^ a2[2] ^ a2[3] ^ 1 ^ 2 ^ 3;
System.out.println(x1);
System.out.println(x2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment