|
@@ -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
|
}
|