|
@@ -1,7 +1,70 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
8
|
+public class ArrayUtility<I> {
|
|
9
|
+
|
|
10
|
+ I[] arr;
|
|
11
|
+ public ArrayUtility(I[] array) {
|
|
12
|
+ this.arr = array;
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ public Integer countDuplicatesInMerge(I[] arrayToMerge, I valueToEvaluate) {
|
|
16
|
+
|
|
17
|
+ Integer count = 0;
|
|
18
|
+ I[] mergedArray = (I[]) new Object[arrayToMerge.length + arr.length];
|
|
19
|
+ System.arraycopy(arr,0,mergedArray,0,arr.length);
|
|
20
|
+ System.arraycopy(arrayToMerge,0,mergedArray,arr.length,arrayToMerge.length);
|
|
21
|
+
|
|
22
|
+ for(int i = 0; i < mergedArray.length; i++){
|
|
23
|
+ if(valueToEvaluate == mergedArray[i]){
|
|
24
|
+ count++;
|
|
25
|
+ }
|
|
26
|
+ }
|
|
27
|
+ return count;
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ public I[] removeValue(I valueToRemove) {
|
|
31
|
+ I[] temp = Arrays.copyOf(arr, arr.length- getNumberOfOccurrences(valueToRemove));
|
|
32
|
+ int index = 0;
|
|
33
|
+ for(int i = 0; i < arr.length; i++){
|
|
34
|
+ if(arr[i] != valueToRemove){
|
|
35
|
+ temp[index] = arr[i];
|
|
36
|
+ index++;
|
|
37
|
+ }
|
|
38
|
+ }
|
|
39
|
+ return temp;
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public I getMostCommonFromMerge(I[] arrayToMerge) {
|
|
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);
|
|
47
|
+
|
|
48
|
+ I mostCommon = mergedArray[0];
|
|
49
|
+ Integer commonCount = getNumberOfOccurrences(mostCommon);
|
|
50
|
+
|
|
51
|
+ for (int i = 0; i < mergedArray.length; i++) {
|
|
52
|
+ Integer tempCount = getNumberOfOccurrences(mergedArray[i]);
|
|
53
|
+ if(tempCount > commonCount){
|
|
54
|
+ mostCommon = mergedArray[i];
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+ return mostCommon;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+ public Integer getNumberOfOccurrences(I valueToEvaluate) {
|
|
62
|
+ Integer count = 0;
|
|
63
|
+ for(I i : arr){
|
|
64
|
+ if(i == valueToEvaluate){
|
|
65
|
+ count++;
|
|
66
|
+ }
|
|
67
|
+ }
|
|
68
|
+ return count;
|
|
69
|
+ }
|
7
|
70
|
}
|