|
@@ -1,5 +1,8 @@
|
1
|
1
|
package rocks.zipcode.io.quiz3.generics;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+import java.util.HashSet;
|
3
|
6
|
import java.util.function.Function;
|
4
|
7
|
|
5
|
8
|
/**
|
|
@@ -13,11 +16,11 @@ public class ArrayUtility<SomeType> {
|
13
|
16
|
}
|
14
|
17
|
|
15
|
18
|
public SomeType findOddOccurringValue() {
|
16
|
|
- return null;
|
|
19
|
+ return findnNumberOccurringValue(1);
|
17
|
20
|
}
|
18
|
21
|
|
19
|
22
|
public SomeType findEvenOccurringValue() {
|
20
|
|
- return null;
|
|
23
|
+ return findnNumberOccurringValue(0);
|
21
|
24
|
}
|
22
|
25
|
|
23
|
26
|
public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
|
|
@@ -35,4 +38,24 @@ public class ArrayUtility<SomeType> {
|
35
|
38
|
public SomeType[] filter(Function<SomeType, Boolean> predicate) {
|
36
|
39
|
return null;
|
37
|
40
|
}
|
|
41
|
+
|
|
42
|
+ public SomeType findnNumberOccurringValue(int evenOrOdd) {
|
|
43
|
+ Arrays.sort(array);
|
|
44
|
+ int currentCount;
|
|
45
|
+ SomeType returnValue = null;
|
|
46
|
+
|
|
47
|
+ for (int i = 0; i < array.length; i++) {
|
|
48
|
+ currentCount = 0;
|
|
49
|
+ for (int j = 0; j < array.length; j++) {
|
|
50
|
+ if (array[j] == array[i]){
|
|
51
|
+ currentCount++;
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+ if (currentCount % 2 == evenOrOdd){
|
|
55
|
+ return array[i];
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ return returnValue;
|
|
60
|
+ }
|
38
|
61
|
}
|