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