|
@@ -1,7 +1,172 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+import java.util.Arrays;
|
|
6
|
+import java.util.LinkedList;
|
|
7
|
+import java.util.List;
|
|
8
|
+
|
3
|
9
|
/**
|
4
|
10
|
* Created by leon on 3/6/18.
|
5
|
11
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
12
|
+public class ArrayUtility<T> {
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+ private T[] inputArray;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+ public ArrayUtility(T[] inputArray) {
|
|
21
|
+ this.inputArray = inputArray;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+ ArrayList<T> newArray = new ArrayList<>();
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
29
|
+
|
|
30
|
+ newArray.addAll(Arrays.asList(arrayToMerge));
|
|
31
|
+ newArray.addAll(Arrays.asList(this.inputArray));
|
|
32
|
+
|
|
33
|
+ int counter = 0;
|
|
34
|
+
|
|
35
|
+ for(int i = 0; i < newArray.size(); i++){
|
|
36
|
+
|
|
37
|
+ if(newArray.get(i).equals(valueToEvaluate)){
|
|
38
|
+ counter++;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ return counter;
|
|
44
|
+
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
54
|
+
|
|
55
|
+ newArray.addAll(Arrays.asList(this.inputArray));
|
|
56
|
+ newArray.addAll(Arrays.asList(arrayToMerge));
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+ int count = 1, tempCount;
|
|
60
|
+
|
|
61
|
+ T mostCommon = newArray.get(0);
|
|
62
|
+
|
|
63
|
+ T temp;
|
|
64
|
+
|
|
65
|
+ for (int i = 0; i < (newArray.size() - 1); i++)
|
|
66
|
+ {
|
|
67
|
+ temp = newArray.get(i);
|
|
68
|
+ tempCount = 0;
|
|
69
|
+ for (int j = 1; j < newArray.size(); j++)
|
|
70
|
+ {
|
|
71
|
+ if (temp == newArray.get(j))
|
|
72
|
+ tempCount++;
|
|
73
|
+ }
|
|
74
|
+ if (tempCount > count)
|
|
75
|
+ {
|
|
76
|
+ mostCommon = temp;
|
|
77
|
+ count = tempCount;
|
|
78
|
+ }
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+ return mostCommon;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+ Integer counter = 0;
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+ for(int i = 0; i < inputArray.length; i++){
|
|
99
|
+
|
|
100
|
+ if(inputArray[i].equals(valueToEvaluate)){
|
|
101
|
+ counter++;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ return counter;
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+ public T[] removeValue(T valueToRemove) {
|
|
120
|
+
|
|
121
|
+ //Convert to String and manipulating
|
|
122
|
+// String str = inputArray.toString();
|
|
123
|
+//
|
|
124
|
+// boolean checkContains = str.contains((CharSequence) valueToRemove);
|
|
125
|
+//
|
|
126
|
+// if(checkContains = true){
|
|
127
|
+// str = str.replace((CharSequence) valueToRemove, "");
|
|
128
|
+// }
|
|
129
|
+//
|
|
130
|
+// T[] returnValue = str;
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+ //Convert to arrayList and manipulating
|
|
134
|
+// ArrayList<T> newArray = new ArrayList<>();
|
|
135
|
+//
|
|
136
|
+// newArray.remove(valueToRemove);
|
|
137
|
+//
|
|
138
|
+//
|
|
139
|
+// T[] returnValue = newArray.toArray();
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+ List<T> returnValue = new LinkedList<>(Arrays.asList(inputArray));
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+ for(int i = 0; i < returnValue.size(); i++){
|
|
147
|
+ if(returnValue.get(i).equals(valueToRemove)){
|
|
148
|
+ returnValue.remove(i);
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+ //return (T[]) returnValue.toArray();
|
|
154
|
+ return returnValue.toArray(Arrays.copyOf(inputArray, returnValue.size()));
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
7
|
172
|
}
|