|
@@ -1,7 +1,92 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.*;
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
8
|
+public class ArrayUtility<T extends Object> {
|
|
9
|
+
|
|
10
|
+ private T[] arr;
|
|
11
|
+
|
|
12
|
+ public ArrayUtility(T[] arr) {
|
|
13
|
+ this.arr = arr;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ private T[] merge(T[] arr1, T[] arr2){
|
|
17
|
+ T[] result = Arrays.copyOf(arr1, arr1.length + arr2.length);
|
|
18
|
+ for(int i = 0; i < arr2.length; i ++){
|
|
19
|
+ result[arr1.length + i] = arr2[i];
|
|
20
|
+ }
|
|
21
|
+ return result;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ private void goThroughArray(Map<T, Integer> count, T[] arr) {
|
|
25
|
+ for(T jawnathan : arr){
|
|
26
|
+ if(!count.containsKey(jawnathan)){
|
|
27
|
+ count.put(jawnathan, 1);
|
|
28
|
+ } else {
|
|
29
|
+ Integer amount = count.get(jawnathan);
|
|
30
|
+ amount ++;
|
|
31
|
+ count.put(jawnathan, amount);
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
37
|
+ T[] array = merge(arr, arrayToMerge);
|
|
38
|
+ Integer count = 0;
|
|
39
|
+ for(T jawn: array){
|
|
40
|
+ if(jawn.equals(valueToEvaluate)){
|
|
41
|
+ count ++;
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+ return count;
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
48
|
+ T[] newArr = merge(arr, arrayToMerge);
|
|
49
|
+ Map<T, Integer> count = new HashMap<>();
|
|
50
|
+
|
|
51
|
+ goThroughArray(count, newArr);
|
|
52
|
+ return countGreatestOccurMap(count);
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ public T countGreatestOccurMap(Map<T, Integer> map){
|
|
56
|
+ int max = 0;
|
|
57
|
+ T result = null;
|
|
58
|
+
|
|
59
|
+ for(T key : map.keySet()){
|
|
60
|
+ if(map.get(key) > max){
|
|
61
|
+ max = map.get(key);
|
|
62
|
+ result = key;
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+ return result;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
69
|
+ Map<T, Integer> count = new HashMap<>();
|
|
70
|
+
|
|
71
|
+ goThroughArray(count, arr);
|
|
72
|
+ return count.get(valueToEvaluate);
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+ public T[] removeValue(T valueToRemove) {
|
|
77
|
+ Integer occ = getNumberOfOccurrences(valueToRemove);
|
|
78
|
+ T[] newArr = (T[]) Array.newInstance(this.arr.getClass().getComponentType(), this.arr.length - occ );
|
|
79
|
+ int count = 0;
|
|
80
|
+ for(T jawnnyboi : arr){
|
|
81
|
+ if(jawnnyboi.equals(valueToRemove)){
|
|
82
|
+ continue;
|
|
83
|
+ }
|
|
84
|
+ else{
|
|
85
|
+ newArr[count] = jawnnyboi;
|
|
86
|
+ count ++;
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+ return newArr;
|
|
90
|
+ }
|
7
|
91
|
}
|
|
92
|
+
|