|
@@ -1,7 +1,78 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
7
|
|
-}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+ public class ArrayUtility<T> {
|
|
11
|
+
|
|
12
|
+ private T[] inputArray;
|
|
13
|
+
|
|
14
|
+ public ArrayUtility(T[] inputArray){
|
|
15
|
+ this.inputArray = inputArray;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public T[] mergeArrays(T[] inputArray, T[] arrayToMerge){
|
|
19
|
+ T[] merged = Arrays.copyOf(inputArray, inputArray.length + arrayToMerge.length);
|
|
20
|
+ System.arraycopy(arrayToMerge, 0, merged, inputArray.length, arrayToMerge.length);
|
|
21
|
+ return merged;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
25
|
+ Integer count = 0;
|
|
26
|
+ for (int i = 0; i < inputArray.length; i++) {
|
|
27
|
+ if (inputArray[i].equals(valueToEvaluate)) {
|
|
28
|
+ count++;
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+ return count;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ public T getMostCommonFromMerge (T[]arrayToMerge){
|
|
36
|
+ T[] mergedArray = mergeArrays(inputArray, arrayToMerge);
|
|
37
|
+
|
|
38
|
+ Integer count = 0;
|
|
39
|
+ T obj = null;
|
|
40
|
+ for (int i = 0; i < mergedArray.length; i++) {
|
|
41
|
+ Integer n = getNumberOfOccurrences(mergedArray[i]);
|
|
42
|
+ if (n > count) {
|
|
43
|
+ count = n;
|
|
44
|
+ obj = mergedArray[i];
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+ return obj;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
51
|
+ T[] mergedArray = mergeArrays(inputArray, (T[]) arrayToMerge);
|
|
52
|
+ Integer count = 0;
|
|
53
|
+ for (int i = 0; i < mergedArray.length; i++) {
|
|
54
|
+ if (mergedArray[i].equals(valueToEvaluate)) {
|
|
55
|
+ count++;
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+ return count;
|
|
59
|
+
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ public T[] removeValue(T valueToRemove) {
|
|
63
|
+
|
|
64
|
+ T[] arrayWithOutRemovedValue;
|
|
65
|
+ arrayWithOutRemovedValue = Arrays.copyOf(inputArray, (inputArray.length - getNumberOfOccurrences(valueToRemove)));
|
|
66
|
+ int idx = 0;
|
|
67
|
+ for (int i = 0; i < inputArray.length; i++) {
|
|
68
|
+ if (!inputArray[i].equals(valueToRemove)) {
|
|
69
|
+ arrayWithOutRemovedValue[idx] = inputArray[i];
|
|
70
|
+ idx++;
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+ return arrayWithOutRemovedValue;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+ }
|
|
78
|
+
|