|
@@ -1,7 +1,73 @@
|
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 {
|
|
8
|
+public class ArrayUtility<T> {
|
|
9
|
+ T[] inputArray;
|
|
10
|
+
|
|
11
|
+ public ArrayUtility(T[] inputArray) {
|
|
12
|
+ this.inputArray = inputArray;
|
|
13
|
+ }
|
|
14
|
+
|
|
15
|
+ public T[] mergeArray(T[] inputArray, T[] arrayToMerge){
|
|
16
|
+ T[] mergedArray = Arrays.copyOf(inputArray, inputArray.length + arrayToMerge.length);
|
|
17
|
+
|
|
18
|
+ System.arraycopy(arrayToMerge, 0, mergedArray, inputArray.length, arrayToMerge.length);
|
|
19
|
+
|
|
20
|
+ return mergedArray;
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
24
|
+ Integer count = 0;
|
|
25
|
+ T[] finalArray = mergeArray(arrayToMerge, inputArray);
|
|
26
|
+ for(int i = 0; i< finalArray.length; i++){
|
|
27
|
+ if(finalArray[i].equals(valueToEvaluate)){
|
|
28
|
+ count ++;
|
|
29
|
+ }
|
|
30
|
+
|
|
31
|
+ }
|
|
32
|
+ return count;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
36
|
+ Integer count = 0;
|
|
37
|
+ T obj = null;
|
|
38
|
+ T[] newArray = mergeArray(inputArray, arrayToMerge);
|
|
39
|
+ for(int i = 0; i < newArray.length; i++){
|
|
40
|
+ Integer a = getNumberOfOccurrences(newArray[i]);
|
|
41
|
+ if(a > count){
|
|
42
|
+ count = a;
|
|
43
|
+ obj = newArray[i];
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ return obj;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
51
|
+ Integer count = 0;
|
|
52
|
+ for(int i = 0; i<inputArray.length; i++){
|
|
53
|
+ if(inputArray[i].equals(valueToEvaluate)){
|
|
54
|
+ count++;
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+ return count;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public T[] removeValue(T valueToRemove) {
|
|
61
|
+ Integer notDuplicates = 0;
|
|
62
|
+ int newSize = inputArray.length - getNumberOfOccurrences(valueToRemove);
|
|
63
|
+ T[] outputArray;
|
|
64
|
+ outputArray = Arrays.copyOf(inputArray, newSize);
|
|
65
|
+ for(int i = 0; i<inputArray.length; i++){
|
|
66
|
+ if(!inputArray[i].equals(valueToRemove)){
|
|
67
|
+ outputArray[notDuplicates]=inputArray[i];
|
|
68
|
+ notDuplicates++;
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+ return outputArray;
|
|
72
|
+ }
|
7
|
73
|
}
|