-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution.java
33 lines (30 loc) · 890 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* DeveloperName(): Jignesh Chudasama
* GithubName(): https://github.com/Jignesh-81726
*/
import java.util.Scanner;
// O(n) runtime. O(1) space. Uses XOR. Keep in mind:
// 1) x ^ x = 0
// 2) x ^ 0 = x
// 3) XOR is commutative and associative
public class Solution {
public static void main(String [] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [] array = new int[n];
for(int i = 0; i < n; i++){
array[i] = scan.nextInt();
}
scan.close();
System.out.println(lonelyInteger(array));
}
/* XORs all numbers in array together */
public static int lonelyInteger(int [] array) {
int val = 0;
for (int num : array) {
val = val ^ num; // ^ is XOR operator
}
return val;
}
}