|
@@ -1,7 +1,68 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
|
- * Created by leon on 3/6/18.
|
|
6
|
+ * filename:
|
|
7
|
+ * project: question1
|
|
8
|
+ * author: https://github.com/vvmk
|
|
9
|
+ * date: 3/5/18
|
5
|
10
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
11
|
+public class ArrayUtility<T> {
|
|
12
|
+ private T[] base;
|
|
13
|
+
|
|
14
|
+ public ArrayUtility(T[] base) {
|
|
15
|
+ this.base = base;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToCheck) {
|
|
19
|
+ mergeArrays(arrayToMerge);
|
|
20
|
+ return getNumberOfOccurrences(valueToCheck);
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
24
|
+ T mostCommon = null;
|
|
25
|
+ int mostOccurrences = 0;
|
|
26
|
+ mergeArrays(arrayToMerge);
|
|
27
|
+ for (T t : base) {
|
|
28
|
+ int count = getNumberOfOccurrences(t);
|
|
29
|
+ if (count > mostOccurrences) {
|
|
30
|
+ mostCommon = t;
|
|
31
|
+ mostOccurrences = count;
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ return mostCommon;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ public int getNumberOfOccurrences(T objectToCheck) {
|
|
38
|
+ int sum = 0;
|
|
39
|
+ for (T t : base) {
|
|
40
|
+ if (objectToCheck.equals(t))
|
|
41
|
+ sum++;
|
|
42
|
+ }
|
|
43
|
+ return sum;
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public T[] removeValue(T objectToRemove) {
|
|
47
|
+ T[] ret = base;
|
|
48
|
+ for (int i = 0; i < base.length; i++) {
|
|
49
|
+ if (base[i].equals(objectToRemove)) {
|
|
50
|
+ ret = Arrays.copyOf(Arrays.copyOfRange(base, 0, i), base.length - 1);
|
|
51
|
+ System.arraycopy(base, i + 1, ret, i, base.length - i - 1);
|
|
52
|
+ base = ret;
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+ return ret;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ protected void mergeArrays(T[] arrayToMerge) {
|
|
59
|
+ int newSize = base.length + arrayToMerge.length;
|
|
60
|
+ base = Arrays.copyOf(base, newSize); // base should be referenceing new array that is identical except length = newSize;
|
|
61
|
+ System.arraycopy(arrayToMerge, 0, base, (newSize - arrayToMerge.length), arrayToMerge.length);
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public T[] getBase() {
|
|
65
|
+ return base;
|
|
66
|
+ }
|
7
|
67
|
}
|
|
68
|
+
|