Created
May 4, 2020 21:50
-
-
Save TheGloriousGenesis/0eba56ddb667541a0b5321470569ad43 to your computer and use it in GitHub Desktop.
Uncoupled Integer
This file contains 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
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int result = 0; | |
while(scanner.hasNext()) { | |
for (String i: scanner.next().split(",")) { | |
/** XOR: when you XOR with a integer an even number of times | |
* your return value will be 0 or whatever you started with. | |
* E.g A ^ B ^ B = A. Hence can iterate through the array and | |
* only odd one out will remain as everything else is in pairs! | |
**/ | |
result = result ^ Integer.parseInt(i.trim()); | |
} | |
} | |
System.out.println(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment