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