nafis nibir 6 years ago
parent
commit
8d9af14d5b
1 changed files with 30 additions and 5 deletions
  1. 30
    5
      src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java

+ 30
- 5
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java View File

@@ -16,12 +16,15 @@ public class ArrayUtility<T> {
16 16
         this.element = array;
17 17
     }
18 18
 
19
-    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
19
+    public T[] merge(T[] arrayToMerge){
20 20
         T[] merged = Stream.concat(Arrays.stream(arrayToMerge), Arrays.stream(element))
21
-                           .toArray(size -> (T[]) Array.newInstance(element.getClass().getComponentType(), element.length + arrayToMerge.length));
21
+                .toArray(size -> (T[]) Array.newInstance(element.getClass().getComponentType(), element.length + arrayToMerge.length));
22
+        return merged;
23
+    }
22 24
 
25
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
23 26
         Integer count = 0;
24
-        for (T element: merged){
27
+        for (T element: merge(arrayToMerge)){
25 28
             if(element.equals(valueToEvaluate)){
26 29
                 count++;
27 30
             }
@@ -42,10 +45,32 @@ public class ArrayUtility<T> {
42 45
 
43 46
 
44 47
     public T getMostCommonFromMerge(T[] arrayToMerge) {
45
-        return null;
48
+        T[] merged = merge(arrayToMerge);
49
+        T thing = null;
50
+        int mostOcc = 0;
51
+        int count = 0;
52
+        for (int i = 0; i < merged.length; i++){
53
+            for (int j = 0; j < merged.length; j++){
54
+                if(merged[i].equals(merged[j])){
55
+                    count++;
56
+                }
57
+            }
58
+            if (count > mostOcc){
59
+                thing = merged[i];
60
+                mostOcc = count;
61
+            }
62
+            count = 0;
63
+        }
64
+        return thing;
46 65
     }
47 66
 
48 67
     public Integer getNumberOfOccurrences(T valueToEvaluate) {
49
-        return null;
68
+        int count = 0;
69
+        for (int i = 0; i < element.length; i++){
70
+            if(element[i].equals(valueToEvaluate)) {
71
+                count++;
72
+            }
73
+        }
74
+        return count;
50 75
     }
51 76
 }