|
@@ -1,5 +1,6 @@
|
1
|
1
|
package rocks.zipcode.io.quiz3.generics;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
3
|
4
|
import java.util.function.Function;
|
4
|
5
|
|
5
|
6
|
/**
|
|
@@ -13,18 +14,32 @@ public class ArrayUtility<SomeType> {
|
13
|
14
|
}
|
14
|
15
|
|
15
|
16
|
public SomeType findOddOccurringValue() {
|
|
17
|
+ for (SomeType thing: array) {
|
|
18
|
+ if(getNumberOfOccurrences(thing)%2==1) return thing;
|
|
19
|
+ }
|
16
|
20
|
return null;
|
17
|
21
|
}
|
18
|
22
|
|
19
|
23
|
public SomeType findEvenOccurringValue() {
|
|
24
|
+ for (SomeType thing: array) {
|
|
25
|
+ if(getNumberOfOccurrences(thing)%2==0) return thing;
|
|
26
|
+ }
|
20
|
27
|
return null;
|
21
|
28
|
}
|
22
|
29
|
|
23
|
30
|
public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
|
24
|
|
- return null;
|
|
31
|
+ Integer i = 0;
|
|
32
|
+ for (SomeType thing: array) {
|
|
33
|
+ if(thing.equals(valueToEvaluate)) i++;
|
|
34
|
+ }
|
|
35
|
+ return i;
|
25
|
36
|
}
|
26
|
37
|
|
27
|
38
|
public SomeType[] filter(Function<SomeType, Boolean> predicate) {
|
28
|
|
- return null;
|
|
39
|
+
|
|
40
|
+ Class classDeclare = array[0].getClass().getComponentType();
|
|
41
|
+ SomeType[] arr = (SomeType[]) Arrays.stream(array).map(predicate).toArray();
|
|
42
|
+
|
|
43
|
+ return arr;
|
29
|
44
|
}
|
30
|
45
|
}
|