|
@@ -1,7 +1,71 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import com.sun.xml.internal.xsom.impl.scd.Iterators;
|
|
4
|
+
|
|
5
|
+import java.lang.reflect.Array;
|
|
6
|
+import java.util.ArrayList;
|
|
7
|
+
|
3
|
8
|
/**
|
4
|
9
|
* Created by leon on 3/6/18.
|
5
|
10
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
11
|
+public class ArrayUtility <T> {
|
|
12
|
+ public T[] newOne;
|
|
13
|
+
|
|
14
|
+ public ArrayUtility(T[] newOne) {
|
|
15
|
+ this.newOne = newOne;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public int getNumberOfOccurrences(T valueToEvaluate) {
|
|
19
|
+
|
|
20
|
+ int count = 0;
|
|
21
|
+ for (T element : newOne) {
|
|
22
|
+ if (valueToEvaluate.equals(element)) {
|
|
23
|
+ count++;
|
|
24
|
+ }
|
|
25
|
+ }
|
|
26
|
+ return count;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+ public Object[] merge(T[] arr2) {
|
|
31
|
+ Object[] merged = new Object[newOne.length + arr2.length];
|
|
32
|
+ System.arraycopy(newOne, 0, merged, 0, newOne.length);
|
|
33
|
+ System.arraycopy(arr2, 0, merged, newOne.length, arr2.length);
|
|
34
|
+ return merged;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
38
|
+ ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
|
|
39
|
+ return arrayUtil.getNumberOfOccurrences(valueToEvaluate);
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
43
|
+ ArrayUtility arrayUtiliz = new ArrayUtility(merge(arrayToMerge));
|
|
44
|
+ int tempCount = 0;
|
|
45
|
+ T mostPopular = null;
|
|
46
|
+ for (Object hold : arrayUtiliz.newOne) {
|
|
47
|
+ if (getNumberOfOccurrences((T) hold) > tempCount) {
|
|
48
|
+ tempCount = getNumberOfOccurrences((T) hold);
|
|
49
|
+ mostPopular = (T) hold;
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+ return mostPopular;
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+ public T[] removeValue(T valueToRemove) {
|
|
57
|
+ //Object [] newArray = new Object[newOne.length-getNumberOfOccurrences(valueToRemove)];
|
|
58
|
+ //int counter = 0;
|
|
59
|
+ ArrayList <T> tempArray = new ArrayList<>();
|
|
60
|
+ for (int i = 0; i < newOne.length; i++) {
|
|
61
|
+ if (!newOne[i].equals(valueToRemove)) {
|
|
62
|
+ tempArray.add(newOne[i]);
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+ int newLength = tempArray.size();
|
|
66
|
+ newOne = (T[]) Array.newInstance(newOne.getClass().getComponentType(), newLength);
|
|
67
|
+ return tempArray.toArray(newOne);
|
|
68
|
+ }
|
7
|
69
|
}
|
|
70
|
+
|
|
71
|
+
|