|
@@ -1,7 +1,103 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+import java.lang.reflect.Array;
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
8
|
+public class ArrayUtility<T> {
|
|
9
|
+
|
|
10
|
+ private T[] inputArray;
|
|
11
|
+
|
|
12
|
+ public ArrayUtility(T[] array)
|
|
13
|
+ {
|
|
14
|
+ this.inputArray = array;
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ public T[] mergeArrays(T[]inputArray, T[] otherArray)
|
|
18
|
+ {
|
|
19
|
+ T[] finalArray = Arrays.copyOf(inputArray, inputArray.length + otherArray.length);
|
|
20
|
+
|
|
21
|
+ System.arraycopy(otherArray, 0, finalArray, inputArray.length, otherArray.length);
|
|
22
|
+
|
|
23
|
+ return finalArray;
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate)
|
|
27
|
+ {
|
|
28
|
+ Integer count = 0;
|
|
29
|
+
|
|
30
|
+ T[] newArray = mergeArrays(inputArray, arrayToMerge);
|
|
31
|
+
|
|
32
|
+ for (int i = 0; i < newArray.length; i++)
|
|
33
|
+ {
|
|
34
|
+ if (newArray[i].equals(valueToEvaluate))
|
|
35
|
+ {
|
|
36
|
+ count++;
|
|
37
|
+ }
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ return count;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ public T getMostCommonFromMerge(T[] arrayToMerge)
|
|
44
|
+ {
|
|
45
|
+ Integer count = 0;
|
|
46
|
+ T obj = null;
|
|
47
|
+
|
|
48
|
+ T[] newArray = mergeArrays(inputArray, arrayToMerge);
|
|
49
|
+
|
|
50
|
+ for (int i = 0; i < newArray.length; i++)
|
|
51
|
+ {
|
|
52
|
+ Integer n = getNumberOfOccurrences(newArray[i]);
|
|
53
|
+
|
|
54
|
+ if (n > count)
|
|
55
|
+ {
|
|
56
|
+ count = n;
|
|
57
|
+ obj = newArray[i];
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ return obj;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate)
|
|
66
|
+ {
|
|
67
|
+ Integer count = 0;
|
|
68
|
+
|
|
69
|
+ for (int i = 0; i < inputArray.length; i++)
|
|
70
|
+ {
|
|
71
|
+ if (inputArray[i].equals(valueToEvaluate))
|
|
72
|
+ {
|
|
73
|
+ count++;
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ return count;
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ public T[] removeValue(T valueToRemove)
|
|
81
|
+ {
|
|
82
|
+ Integer size;
|
|
83
|
+ size = inputArray.length - getNumberOfOccurrences(valueToRemove);
|
|
84
|
+
|
|
85
|
+ T[] newArray;
|
|
86
|
+ newArray = Arrays.copyOf(inputArray, size);
|
|
87
|
+
|
|
88
|
+ int j = 0;
|
|
89
|
+
|
|
90
|
+ for (int i = 0; i < inputArray.length; i++)
|
|
91
|
+ {
|
|
92
|
+ if (!inputArray[i].equals(valueToRemove))
|
|
93
|
+ {
|
|
94
|
+ newArray[j] = inputArray[i];
|
|
95
|
+ j++;
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ return newArray;
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+
|
7
|
103
|
}
|