|
@@ -1,7 +1,104 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import javax.jws.Oneway;
|
|
4
|
+import java.lang.reflect.Array;
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+
|
3
|
7
|
/**
|
4
|
8
|
* Created by leon on 3/6/18.
|
5
|
9
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
10
|
+public class ArrayUtility <T> {
|
|
11
|
+
|
|
12
|
+ T[] list;
|
|
13
|
+
|
|
14
|
+ public ArrayUtility(T[] list) {
|
|
15
|
+
|
|
16
|
+ this.list = list;
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+ public int getNumberOfOccurrences(T element){
|
|
21
|
+
|
|
22
|
+ int count = 0;
|
|
23
|
+
|
|
24
|
+ for( T el : list) {
|
|
25
|
+
|
|
26
|
+ if(el.equals(element)) {
|
|
27
|
+ count++;
|
|
28
|
+ }
|
|
29
|
+ }
|
|
30
|
+ return count;
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ public Object[] merge(T[] arr1) {
|
|
34
|
+
|
|
35
|
+ Object[] mergeArr = new Object[list.length + arr1.length];
|
|
36
|
+
|
|
37
|
+ int counter = 0;
|
|
38
|
+
|
|
39
|
+ for(T el : list) {
|
|
40
|
+
|
|
41
|
+ mergeArr[counter] = el;
|
|
42
|
+
|
|
43
|
+ counter++;
|
|
44
|
+ }
|
|
45
|
+ for(T el : arr1) {
|
|
46
|
+
|
|
47
|
+ mergeArr[counter] = el;
|
|
48
|
+
|
|
49
|
+ counter++;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ return mergeArr;
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
57
|
+
|
|
58
|
+ ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
|
|
59
|
+
|
|
60
|
+ return arrayUtil.getNumberOfOccurrences(valueToEvaluate);
|
|
61
|
+
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
65
|
+
|
|
66
|
+ ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
|
|
67
|
+
|
|
68
|
+ int tempCount = 0;
|
|
69
|
+
|
|
70
|
+ T mostCommonObj = null;
|
|
71
|
+
|
|
72
|
+ for(Object el : arrayUtil.list) {
|
|
73
|
+
|
|
74
|
+ if(getNumberOfOccurrences((T)el) > tempCount) {
|
|
75
|
+
|
|
76
|
+ tempCount = getNumberOfOccurrences((T)el);
|
|
77
|
+
|
|
78
|
+ mostCommonObj = (T)el;
|
|
79
|
+ }
|
|
80
|
+ }
|
|
81
|
+ return mostCommonObj;
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public T[] removeValue(T valueToRemove) {
|
|
85
|
+
|
|
86
|
+ ArrayList<T> tempArr = new ArrayList<>();
|
|
87
|
+
|
|
88
|
+ int counter = 0;
|
|
89
|
+
|
|
90
|
+ for(int i = 0; i < list.length; i++){
|
|
91
|
+
|
|
92
|
+ if(!list[i].equals(valueToRemove)) {
|
|
93
|
+
|
|
94
|
+ tempArr.add(list[i]);
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+ }
|
|
98
|
+ }
|
|
99
|
+ int newLength = tempArr.size();
|
|
100
|
+ list = (T[]) Array.newInstance(list.getClass().getComponentType(),newLength);
|
|
101
|
+
|
|
102
|
+ return tempArr.toArray(list);
|
|
103
|
+ }
|
7
|
104
|
}
|