Last active
July 24, 2019 00:01
-
-
Save feuyeux/58b72ac111b47a9a6e78f52bcba18e18 to your computer and use it in GitHub Desktop.
exclusive_or_self-inverse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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