|
@@ -1,7 +1,126 @@
|
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
|
+
|
3
|
8
|
/**
|
4
|
9
|
* Created by leon on 3/6/18.
|
5
|
10
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
11
|
+public class ArrayUtility<T> {
|
|
12
|
+
|
|
13
|
+ private T[] arrayToMerge;
|
|
14
|
+ private T valueToEvaluate;
|
|
15
|
+ private T[] inputArray;
|
|
16
|
+
|
|
17
|
+ //constructor
|
|
18
|
+ public ArrayUtility(T[] inputArray) {
|
|
19
|
+ this.inputArray = inputArray;
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+ //methods
|
|
24
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
25
|
+ Integer count = 0;
|
|
26
|
+ Integer duplicateCount = 0;
|
|
27
|
+
|
|
28
|
+ T[] mergedArr = Arrays.copyOf(arrayToMerge, arrayToMerge.length+inputArray.length );
|
|
29
|
+ for (int i = 0; i < arrayToMerge.length; i++) {
|
|
30
|
+ mergedArr[i] = arrayToMerge[i];
|
|
31
|
+ count++;
|
|
32
|
+ }
|
|
33
|
+ for (int j = 0; j < inputArray.length; j++) {
|
|
34
|
+ mergedArr[count++] = inputArray[j];
|
|
35
|
+ }
|
|
36
|
+ for (T thing : mergedArr) {
|
|
37
|
+ if (thing == valueToEvaluate) {
|
|
38
|
+ duplicateCount++;
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+ return duplicateCount;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+// List myArrays = new ArrayList((Arrays.asList(inputArray)));
|
|
47
|
+// myArrays.addAll(Arrays.asList(arrayToMerge));
|
|
48
|
+// T[]mergedArray = myArrays.toArray();
|
|
49
|
+
|
|
50
|
+ //this part works
|
|
51
|
+//
|
|
52
|
+// for (T thing : arrayToMerge) {
|
|
53
|
+// if (thing == valueToEvaluate) {
|
|
54
|
+// count++;
|
|
55
|
+// }
|
|
56
|
+// }
|
|
57
|
+// for (T thingtwo : inputArray) {
|
|
58
|
+// if (thingtwo == valueToEvaluate) {
|
|
59
|
+// count++;
|
|
60
|
+// }
|
|
61
|
+// }
|
|
62
|
+// return count;
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
67
|
+ Integer count = 0;
|
|
68
|
+
|
|
69
|
+ for (int i = 0; i < inputArray.length; i++){
|
|
70
|
+ if (inputArray[i] == valueToEvaluate){
|
|
71
|
+ count++;
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+ return count;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+ public T[] removeValue(T valueToRemove) {
|
|
80
|
+ Integer adjustedlength;
|
|
81
|
+
|
|
82
|
+ adjustedlength = inputArray.length - getNumberOfOccurrences(valueToRemove);
|
|
83
|
+ T[] scrubbedArray = Arrays.copyOf(inputArray, adjustedlength);
|
|
84
|
+
|
|
85
|
+ int j = 0;
|
|
86
|
+ for (int i = 0; i < inputArray.length; i++){
|
|
87
|
+ if (inputArray[i] != valueToRemove){
|
|
88
|
+ scrubbedArray[j] = inputArray[i];
|
|
89
|
+ j++;
|
|
90
|
+ }
|
|
91
|
+ }
|
|
92
|
+ return scrubbedArray;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
97
|
+ int commoncount = 0;
|
|
98
|
+ int count = 0;
|
|
99
|
+ T obj = null;
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+ T[] mergedArr = Arrays.copyOf(arrayToMerge, arrayToMerge.length+inputArray.length );
|
|
103
|
+ for (int i = 0; i < arrayToMerge.length; i++) {
|
|
104
|
+ mergedArr[i] = arrayToMerge[i];
|
|
105
|
+ count++;
|
|
106
|
+ }
|
|
107
|
+ for (int j = 0; j < inputArray.length; j++) {
|
|
108
|
+ mergedArr[count++] = inputArray[j];
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ for (int i = 0; i < mergedArr.length; i++){
|
|
112
|
+ int n = getNumberOfOccurrences(mergedArr[i]);
|
|
113
|
+
|
|
114
|
+ if (n >commoncount){
|
|
115
|
+ commoncount = n;
|
|
116
|
+ obj = mergedArr[i];
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ }
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+ return obj;
|
|
123
|
+ }
|
7
|
124
|
}
|
|
125
|
+
|
|
126
|
+
|