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