|
@@ -1,7 +1,81 @@
|
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
|
+ private T[] array;
|
|
11
|
+
|
|
12
|
+ public ArrayUtility(T[] array) {
|
|
13
|
+ this.array = array;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
17
|
+ merge(arrayToMerge);
|
|
18
|
+ int count = 0;
|
|
19
|
+ for (int i = 0; i < array.length; i++) {
|
|
20
|
+ if (array[i].equals(valueToEvaluate)) {
|
|
21
|
+ count++;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ return count;
|
|
25
|
+ }
|
|
26
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
27
|
+ merge(arrayToMerge);
|
|
28
|
+ int maxCount = 0;
|
|
29
|
+ T commonValue = null;
|
|
30
|
+ for (int i = 0; i < array.length; i++) {
|
|
31
|
+ T currValue = array[i];
|
|
32
|
+ int currCount = 0;
|
|
33
|
+ for (int j = i + 1; j < array.length; j++) {
|
|
34
|
+ if (array[j].equals(currValue)) currCount++;
|
|
35
|
+ }
|
|
36
|
+ if (currCount > maxCount || commonValue == null) {
|
|
37
|
+ maxCount = currCount;
|
|
38
|
+ commonValue = currValue;
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+ return commonValue;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ private void merge(T[] arrayToMerge) {
|
|
45
|
+ T[] temp = (T[]) new Object[arrayToMerge.length + array.length];
|
|
46
|
+ for (int i = 0; i < arrayToMerge.length + array.length; i++) {
|
|
47
|
+ if (i < array.length) {
|
|
48
|
+ temp[i] = array[i];
|
|
49
|
+ }
|
|
50
|
+ else {
|
|
51
|
+ temp[i] = arrayToMerge[i - array.length];
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+ array = temp;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
59
|
+ int count = 0;
|
|
60
|
+ for (int i = 0; i < array.length; i++) {
|
|
61
|
+ if (array[i].equals(valueToEvaluate)) {
|
|
62
|
+ count++;
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+ return count;
|
|
66
|
+ }
|
|
67
|
+ public T[] removeValue(T valueToRemove) {
|
|
68
|
+ int numOccurrences = getNumberOfOccurrences(valueToRemove);
|
|
69
|
+ T[] temp = Arrays.copyOf(array, array.length - numOccurrences);
|
|
70
|
+ int removed = 0;
|
|
71
|
+ for (int i = 0; i < array.length; i++) {
|
|
72
|
+ if (!array[i].equals(valueToRemove)) {
|
|
73
|
+ temp[i - removed] = array[i];
|
|
74
|
+ }
|
|
75
|
+ else removed++;
|
|
76
|
+ }
|
|
77
|
+ array = temp;
|
|
78
|
+ return array;
|
|
79
|
+ }
|
|
80
|
+
|
7
|
81
|
}
|