-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoddInt.java
More file actions
48 lines (45 loc) · 1.19 KB
/
oddInt.java
File metadata and controls
48 lines (45 loc) · 1.19 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
public class oddInt {
// xor: 101011
// N: 111000
// xor: 010011
// xor ^ N ^ N = xor
// 1 3 1
// 0 ^ 1 ^ 3 ^ 1 = ?
// 0 ^ (1 ^ 1) ^ 3 = ?
// 0 ^ 0 ^ 3 = 3
// xor ^= N
//
public static int findIt(int[] a) {
int occurance = 0;
int curNum = 0;
// List<Integer> b = Arrays.stream(a)
// .boxed()
// .sorted()
// .collect(Collectors.toList());
Arrays.sort(a);
for(int i = 0; i < a.length; i++){
// loop state: occurance , curNum, element, i
//
if (i == 0) {
occurance = 1;
curNum = a[i];
continue;
}
if(curNum != a[i]){
if(occurance % 2 == 1){
return curNum;
}
occurance = 1;
curNum = a[i];
} else{
occurance++;
}
}
return curNum;
}
public static void main(String[] args) {
int[] arr = new int[] {1, 1, 5, 5, 5, 5, 7, 7, 7};
System.out.println(findIt(arr));
}
}