-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoddoccurencesinarray.java
More file actions
35 lines (33 loc) · 1.09 KB
/
oddoccurencesinarray.java
File metadata and controls
35 lines (33 loc) · 1.09 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
public int solution (int[] A){
int a = 0;
//sort the array
Arrays.sort(A);
int[] sortA = A;
int x = 0;
int cnt = 1;
int[] oddA = new int[]{};
//int idxOdd = 0;
while ( x < sortA.length ){
if ( x != 0 ){
if (a == sortA[x]){
cnt++;
} else {
if (cnt % 2 > 0){
oddA = Arrays.copyOf(oddA, oddA.length + 1 ); //sortA[ x - 1];
oddA[oddA.length - 1] = sortA [x - 1];
}
a = sortA[x];
cnt = 1;
}
} else {
a = sortA[x];
}
x++;
}
if ( x == sortA.length && cnt == 1) {
oddA = Arrays.copyOf(oddA, oddA.length + 1 ); //sortA[ x - 1];
oddA[oddA.length - 1] = sortA[x - 1];
}
System.err.println("sortA: " + Arrays.toString(sortA) + System.lineSeparator() + "Result: " + Arrays.toString(oddA));
return oddA[0];
};