|
@@ -1,7 +1,120 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+import java.util.List;
|
|
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[] genericArray;
|
|
13
|
+
|
|
14
|
+ public ArrayUtility(T[] inputArray) {
|
|
15
|
+ this.genericArray = inputArray;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public Integer getNumberOfOccurrences(T valToCheck) {
|
|
19
|
+
|
|
20
|
+ int numOfOccurrences = 0;
|
|
21
|
+
|
|
22
|
+ for (T element: genericArray) {
|
|
23
|
+ if (element.equals(valToCheck)){
|
|
24
|
+ numOfOccurrences++;
|
|
25
|
+ }
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ return numOfOccurrences;
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ public T[] removeValue(T valueToRemove) {
|
|
34
|
+
|
|
35
|
+// List<T> tempArrayList = new ArrayList<T>();
|
|
36
|
+//
|
|
37
|
+// for (T element: genericArray){
|
|
38
|
+// if (!element.equals(valueToRemove)){
|
|
39
|
+// tempArrayList.add(element);
|
|
40
|
+// }
|
|
41
|
+// }
|
|
42
|
+
|
|
43
|
+ T[] arrayWithValueRemoved = Arrays.copyOf(genericArray, 0);
|
|
44
|
+ for (int i = 0; i < genericArray.length; i++) {
|
|
45
|
+ if (!genericArray[i].equals(valueToRemove)) {
|
|
46
|
+ arrayWithValueRemoved = Arrays.copyOf(arrayWithValueRemoved, arrayWithValueRemoved.length + 1);
|
|
47
|
+ arrayWithValueRemoved[arrayWithValueRemoved.length - 1] = genericArray[i];
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ return arrayWithValueRemoved;
|
|
51
|
+
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToCheck) {
|
|
55
|
+
|
|
56
|
+ Integer numOfDuplicates = 0;
|
|
57
|
+
|
|
58
|
+ for (int i = 0; i < genericArray.length; i++) {
|
|
59
|
+ if (genericArray[i].equals(valueToCheck)) {
|
|
60
|
+ numOfDuplicates++;
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ for (int j = 0; j < arrayToMerge.length; j++) {
|
|
65
|
+ if (arrayToMerge[j].equals(valueToCheck)) {
|
|
66
|
+ numOfDuplicates++;
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ return numOfDuplicates;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ public T getMostCommonFromMerge(T[] secondArray) {
|
|
74
|
+
|
|
75
|
+ Integer largestWeveSeenSoFar = 0;
|
|
76
|
+ T mostCommonObject = null;
|
|
77
|
+
|
|
78
|
+ for (int i = 0; i < this.genericArray.length; i++) {
|
|
79
|
+ if (countDuplicatesInMerge(secondArray, this.genericArray[i]) > largestWeveSeenSoFar) {
|
|
80
|
+ mostCommonObject = this.genericArray[i];
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ return mostCommonObject;
|
|
85
|
+ }
|
|
86
|
+
|
7
|
87
|
}
|
|
88
|
+
|
|
89
|
+// public T[] mergeArrays(T[] arrayToAdd) {
|
|
90
|
+//
|
|
91
|
+// List<T> amysList = new ArrayList<T>();
|
|
92
|
+//
|
|
93
|
+// for (T element: genericArray){
|
|
94
|
+// amysList.add(element);
|
|
95
|
+// }
|
|
96
|
+// for (T element: arrayToAdd){
|
|
97
|
+// amysList.add(element);
|
|
98
|
+// }
|
|
99
|
+//
|
|
100
|
+// T[] answerArray = Arrays.copyOf(genericArray, );
|
|
101
|
+// for (int i = 0; i < answerArray.length; i++){
|
|
102
|
+// answerArray[i] = amysList.get(i);
|
|
103
|
+// }
|
|
104
|
+// return answerArray;
|
|
105
|
+//
|
|
106
|
+// }
|
|
107
|
+//}
|
|
108
|
+
|
|
109
|
+// public T[] removeValue(T valueToRemove) {
|
|
110
|
+//
|
|
111
|
+// T[] arrayWithValueRemoved = new T[genericArray.length];
|
|
112
|
+// for (int i = 0; i < arrayWithValueRemoved.length; i++){
|
|
113
|
+// arrayWithValueRemoved[i] = genericArray[i];
|
|
114
|
+// }
|
|
115
|
+// return arrayWithValueRemoved;
|
|
116
|
+//
|
|
117
|
+// }
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|