|
@@ -1,7 +1,81 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
|
2
|
+import java.util.ArrayList;
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+import java.util.HashMap;
|
|
5
|
+import java.util.Map;
|
|
6
|
+
|
|
7
|
+import static java.util.Arrays.*;
|
|
8
|
+
|
2
|
9
|
|
3
|
10
|
/**
|
4
|
11
|
* Created by leon on 3/6/18.
|
5
|
12
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
13
|
+public class ArrayUtility<T> {
|
|
14
|
+
|
|
15
|
+ private ArrayList<T> inputList = new ArrayList<>();
|
|
16
|
+ private T[] newArray;
|
|
17
|
+
|
|
18
|
+ public ArrayUtility(T[] inputArray) {
|
|
19
|
+ inputList.addAll(asList(inputArray));
|
|
20
|
+ this.newArray = copyOf(inputArray, inputArray.length);
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate){
|
|
24
|
+ inputList.addAll(asList(arrayToMerge));
|
|
25
|
+
|
|
26
|
+ Integer counter = 0;
|
|
27
|
+ for(T t : inputList){
|
|
28
|
+ if(t.equals(valueToEvaluate)){
|
|
29
|
+ counter++;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ }return counter;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public T[] removeValue(T valueToRemove){
|
|
36
|
+ T[] myArray = copyOf(newArray, newArray.length);
|
|
37
|
+ T[] lastArray = null;
|
|
38
|
+ for(int i = 0; i < myArray.length; i++){
|
|
39
|
+ if(myArray[i] == valueToRemove){
|
|
40
|
+ System.arraycopy(myArray, i+1, myArray, i, myArray.length - i - 1);
|
|
41
|
+ }
|
|
42
|
+ }
|
|
43
|
+ lastArray = Arrays.copyOf(myArray, myArray.length - getNumberOfOccurrences(valueToRemove));
|
|
44
|
+ return lastArray;
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
48
|
+ inputList.addAll(asList(arrayToMerge));
|
|
49
|
+
|
|
50
|
+ Map<T, Integer> inputMap = new HashMap<>(inputList.size());
|
|
51
|
+ int maxCount;
|
|
52
|
+ T mostCommon = null;
|
|
53
|
+ for (T t : inputList) {
|
|
54
|
+ if (!inputMap.containsKey(t)) {
|
|
55
|
+ inputMap.put(t, 1);
|
|
56
|
+ } else {
|
|
57
|
+ inputMap.put(t, inputMap.get(t) + 1);
|
|
58
|
+ }
|
|
59
|
+ maxCount = 0;
|
|
60
|
+ for (Map.Entry<T, Integer> entry : inputMap.entrySet()) {
|
|
61
|
+ if (entry.getValue() > maxCount) {
|
|
62
|
+ maxCount = entry.getValue();
|
|
63
|
+ mostCommon = entry.getKey();
|
|
64
|
+ }
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+ return mostCommon;
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate){
|
|
71
|
+ Integer counter = 0;
|
|
72
|
+ for(T t : inputList){
|
|
73
|
+ if(t == null){
|
|
74
|
+ counter = 1;
|
|
75
|
+ }else if(t.equals(valueToEvaluate)){
|
|
76
|
+ counter++;
|
|
77
|
+ }
|
|
78
|
+ }return counter;
|
|
79
|
+ }
|
|
80
|
+
|
7
|
81
|
}
|