Sfoglia il codice sorgente

array utility done but filter tests arent annotated?

Jennifer Chao 6 anni fa
parent
commit
56d838e710

+ 38
- 0
src/main/java/rocks/zipcode/io/quiz3/generics/ArrayUtility.java Vedi File

@@ -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,55 @@ public class ArrayUtility<SomeType> {
13 14
     }
14 15
 
15 16
     public SomeType findOddOccurringValue() {
17
+        Integer[] count = countOccurences();
18
+
19
+        for (int i = 0; i < count.length; i++) {
20
+            if (count[i] % 2 != 0) {
21
+                return array[i];
22
+            }
23
+        }
16 24
         return null;
17 25
     }
18 26
 
19 27
     public SomeType findEvenOccurringValue() {
28
+        Integer[] count = countOccurences();
29
+
30
+        for (int i = 0; i < count.length; i++) {
31
+            if (count[i] % 2 == 0) {
32
+                return array[i];
33
+            }
34
+        }
20 35
         return null;
21 36
     }
22 37
 
23 38
     public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
39
+        Integer[] count = countOccurences();
40
+
41
+        for (int i = 0; i < array.length; i++) {
42
+            if (array[i].equals(valueToEvaluate)) {
43
+                return count[i];
44
+            }
45
+        }
24 46
         return null;
25 47
     }
26 48
 
27 49
     public SomeType[] filter(Function<SomeType, Boolean> predicate) {
28 50
         return null;
29 51
     }
52
+
53
+    public Integer[] countOccurences() {
54
+        Integer[] count = new Integer[array.length];
55
+        Arrays.fill(count, 0);
56
+
57
+        for (int i = 0; i < array.length; i++) {
58
+            for (int j = 0; j < array.length; j++) {
59
+                if (array[i].equals(array[j])) {
60
+                    count[i]++;
61
+                }
62
+            }
63
+        }
64
+
65
+        return count;
66
+    }
67
+
30 68
 }