|
@@ -1,7 +1,59 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.*;
|
|
5
|
+
|
3
|
6
|
/**
|
4
|
7
|
* Created by leon on 3/6/18.
|
5
|
8
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
9
|
+public class ArrayUtility<T> {
|
|
10
|
+ private List<T> list;
|
|
11
|
+ private T[] inputArray;
|
|
12
|
+
|
|
13
|
+ public ArrayUtility(T[] inputArray){
|
|
14
|
+ this.list = new ArrayList<>(Arrays.asList(inputArray));
|
|
15
|
+ this.inputArray = inputArray;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public void merge(T[] arrayToMerge){
|
|
19
|
+ list.addAll(new ArrayList<>(Arrays.asList(arrayToMerge)));
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate){
|
|
23
|
+ merge(arrayToMerge);
|
|
24
|
+ return getNumberOfOccurrences(valueToEvaluate);
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
28
|
+ merge(arrayToMerge);
|
|
29
|
+ int most = 0;
|
|
30
|
+ T mostCommon = list.get(0);
|
|
31
|
+ for (T t : list){
|
|
32
|
+ int count = getNumberOfOccurrences(t);
|
|
33
|
+ if (count>most) {
|
|
34
|
+ most = count;
|
|
35
|
+ mostCommon = t;
|
|
36
|
+ }
|
|
37
|
+ }
|
|
38
|
+ return mostCommon;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
42
|
+ int count = 0;
|
|
43
|
+ for (T t : list) {
|
|
44
|
+ if (t.equals(valueToEvaluate)) count++;
|
|
45
|
+ }
|
|
46
|
+ return count;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ public T[] removeValue(T valueToRemove) {
|
|
50
|
+ list.removeIf(e -> e.equals(valueToRemove));
|
|
51
|
+
|
|
52
|
+ Class tClass = inputArray.getClass();
|
|
53
|
+ Class compType = tClass.getComponentType();
|
|
54
|
+ T[] array = (T[]) Array.newInstance(compType,(list.size()-getNumberOfOccurrences(valueToRemove)));
|
|
55
|
+ for(int i=0; i<array.length; i++)
|
|
56
|
+ array[i] = list.get(i);
|
|
57
|
+ return array;
|
|
58
|
+ }
|
7
|
59
|
}
|