David Thornley hace 6 años
padre
commit
1f924e3248

+ 21
- 5
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Ver fichero

@@ -41,17 +41,16 @@ public class ArrayUtility<I> {
41 41
 
42 42
     public I getMostCommonFromMerge(I[] arrayToMerge) {
43 43
 
44
-        I[] mergedArray = (I[]) new Object[arrayToMerge.length + arr.length];
45
-        System.arraycopy(arr,0,mergedArray,0,arr.length);
46
-        System.arraycopy(arrayToMerge,0,mergedArray,arr.length,arrayToMerge.length);
44
+        I[] mergedArray = mergeArrays(arr,arrayToMerge);
47 45
 
48 46
         I mostCommon = mergedArray[0];
49
-        Integer commonCount = getNumberOfOccurrences(mostCommon);
47
+        Integer commonCount = betterGetNumberOfOccurrences(mostCommon, mergedArray);
50 48
 
51 49
         for (int i = 0; i < mergedArray.length; i++) {
52
-            Integer tempCount = getNumberOfOccurrences(mergedArray[i]);
50
+            Integer tempCount = betterGetNumberOfOccurrences(mergedArray[i], mergedArray);
53 51
             if(tempCount > commonCount){
54 52
                 mostCommon = mergedArray[i];
53
+                commonCount = betterGetNumberOfOccurrences(mergedArray[i], mergedArray);
55 54
             }
56 55
         }
57 56
         return mostCommon;
@@ -67,4 +66,21 @@ public class ArrayUtility<I> {
67 66
         }
68 67
         return count;
69 68
     }
69
+
70
+    public I[] mergeArrays(I[] array1, I[] array2){
71
+        I[] mergedArray = (I[]) new Object[array2.length + array1.length];
72
+        System.arraycopy(array1,0, mergedArray,0,array1.length);
73
+        System.arraycopy(array2,0, mergedArray, array1.length, array2.length);
74
+        return mergedArray;
75
+    }
76
+
77
+    public Integer betterGetNumberOfOccurrences(I valueToEvaluate, I[] arr) {
78
+        Integer count = 0;
79
+        for(I i : arr){
80
+            if(i == valueToEvaluate){
81
+                count++;
82
+            }
83
+        }
84
+        return count;
85
+    }
70 86
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/GetMostCommonFromMergeTest.java Ver fichero

@@ -28,7 +28,7 @@ public class GetMostCommonFromMergeTest {
28 28
         // Given
29 29
         Long expected = 8L;
30 30
         Long[] inputArray = {1L, 2L, expected, 8L, 4L, 5L, expected, 0L, 9L, 8L, expected};
31
-        Long[] arrayToMerge = {1L, 2L, expected, 8L, 4L, 5L, expected, 0L, 9L, 8L, expected};
31
+        Long[] arrayToMerge = {1L, 2L, expected, 8L, 4L, 5L, expected, 0L, 9L, expected, 8L};
32 32
         ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
33 33
 
34 34
         // When