|
@@ -1,7 +1,89 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.*;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
8
|
+public class ArrayUtility<T> {
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+ private T[] inputArray;
|
|
12
|
+
|
|
13
|
+ public ArrayUtility(T[] inputArray) {
|
|
14
|
+ this.inputArray = inputArray;
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ public T[] mergeArray(T[] arrayToMerge, T[] inputArray) {
|
|
18
|
+ T[] result = Arrays.copyOf(arrayToMerge, arrayToMerge.length + inputArray.length);
|
|
19
|
+ System.arraycopy(inputArray, 0, result, arrayToMerge.length, inputArray.length);
|
|
20
|
+ return result;
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
24
|
+ T[] thisArray = mergeArray(arrayToMerge, inputArray);
|
|
25
|
+ Map<T, Integer> map = new TreeMap<>();
|
|
26
|
+
|
|
27
|
+ for (T mostCommon : thisArray) {
|
|
28
|
+ //iterate through array to see if how many times entered.
|
|
29
|
+ if (map.containsKey(mostCommon)) {
|
|
30
|
+ map.put(mostCommon, map.get(mostCommon) + 1);
|
|
31
|
+ //keep adding to value if you keep seeing it
|
|
32
|
+ } else {
|
|
33
|
+ map.put(mostCommon, 1);
|
|
34
|
+
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ }
|
|
38
|
+ return (Collections.max(map.entrySet(), Map.Entry.comparingByValue())
|
|
39
|
+ .getKey());
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
43
|
+ int counter = 0;
|
|
44
|
+ T[] thisArray = mergeArray(arrayToMerge, inputArray);
|
|
45
|
+ for (int i = 0; i < thisArray.length; i++) {
|
|
46
|
+ if (thisArray[i].equals(valueToEvaluate)) {
|
|
47
|
+ counter++;
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ return counter;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
54
|
+ int count = 0;
|
|
55
|
+ for (int i = 0; i < inputArray.length; i++) {
|
|
56
|
+ if (inputArray[i].equals(valueToEvaluate))
|
|
57
|
+ count++;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ return count;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ public T[] removeValue(T valueToRemove) {
|
|
64
|
+ //create a copy of the array making its new length # of occurances -valueToremove
|
|
65
|
+ T[] copyArray = Arrays.copyOf(inputArray, inputArray.length-(getNumberOfOccurrences(valueToRemove)));
|
|
66
|
+ //have to declare the length of the new array...so keep count
|
|
67
|
+ int lengthOfNewArray = 0;
|
|
68
|
+ for(int i = 0; i < inputArray.length; i++){
|
|
69
|
+ //put the values that arent the one to remove in new array
|
|
70
|
+ if (!inputArray[i].equals(valueToRemove)){
|
|
71
|
+ copyArray[lengthOfNewArray] = inputArray[i];
|
|
72
|
+ lengthOfNewArray++;
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+ return copyArray;
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+ //CANT INSTANTIATE A GENERIC ARRAY!
|
|
79
|
+// ArrayList<T> list = new ArrayList<>(Arrays.asList(inputArray));
|
|
80
|
+// for (int i = 0; i < list.size(); i++) {
|
|
81
|
+// if (list.get(i).equals(valueToRemove)) {
|
|
82
|
+// list.remove(i);
|
|
83
|
+// return list.toArray(new <T> T[0]);
|
|
84
|
+// }
|
|
85
|
+// }
|
|
86
|
+// return list.toArray(new <T> T[0]);
|
|
87
|
+ }
|
7
|
88
|
}
|
|
89
|
+
|