|
@@ -1,7 +1,72 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+import java.util.Arrays;
|
|
6
|
+import java.util.List;
|
|
7
|
+import java.util.TreeMap;
|
|
8
|
+
|
3
|
9
|
/**
|
4
|
10
|
* Created by leon on 3/6/18.
|
5
|
11
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
12
|
+public class ArrayUtility <T> {
|
|
13
|
+ private T[] tArray;
|
|
14
|
+
|
|
15
|
+ public ArrayUtility(T[] tArray){
|
|
16
|
+ this.tArray = tArray;
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate){
|
|
20
|
+ T[] merged = mergeArrays(arrayToMerge);
|
|
21
|
+ this.tArray = merged;
|
|
22
|
+ return getNumberOfOccurrences(valueToEvaluate);
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ public T getMostCommonFromMerge(T[] arrayToMerge){
|
|
26
|
+ T[] merged = mergeArrays(arrayToMerge);
|
|
27
|
+ int maxDupes = 0;
|
|
28
|
+ T mostCommon = null;
|
|
29
|
+ for (T t:merged) {
|
|
30
|
+ int currentDupes = getNumberOfOccurrencesCustom(t, merged);
|
|
31
|
+ if (currentDupes > maxDupes){
|
|
32
|
+ maxDupes = currentDupes;
|
|
33
|
+ mostCommon = t;
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ return mostCommon;
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ public int getNumberOfOccurrences(T duplicatesToCount){
|
|
40
|
+ return getNumberOfOccurrencesCustom(duplicatesToCount, tArray);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ public int getNumberOfOccurrencesCustom(T duplicatesToCount, T[] testArray){
|
|
44
|
+ int counter = 0;
|
|
45
|
+ for (T t:testArray) {
|
|
46
|
+ if (t.equals(duplicatesToCount)) counter++;
|
|
47
|
+ }
|
|
48
|
+ return counter;
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ public T[] removeValue(T valueToRemove){
|
|
52
|
+ T[] returnArray = Arrays.copyOf(tArray, (tArray.length - this.getNumberOfOccurrences(valueToRemove)));
|
|
53
|
+ int index = 0;
|
|
54
|
+ for (T t:this.tArray) {
|
|
55
|
+ if (!t.equals(valueToRemove)){
|
|
56
|
+ returnArray[index] = t;
|
|
57
|
+ index++;
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ return returnArray;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ public T[] mergeArrays(T[] newArray){
|
|
64
|
+ T[] returnArray = Arrays.copyOf(tArray, (tArray.length + newArray.length));
|
|
65
|
+ int index = tArray.length;
|
|
66
|
+ for (T t:newArray){
|
|
67
|
+ returnArray[index] = t;
|
|
68
|
+ index++;
|
|
69
|
+ }
|
|
70
|
+ return returnArray;
|
|
71
|
+ }
|
7
|
72
|
}
|