|
@@ -1,7 +1,78 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.*;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
8
|
+public class ArrayUtility<E> {
|
|
9
|
+
|
|
10
|
+ private E[] inputArray;
|
|
11
|
+
|
|
12
|
+ public ArrayUtility(E[] inputArray) {
|
|
13
|
+ this.inputArray = inputArray;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public int countDuplicatesInMerge(E[] arrayToMerge, E valueToEvaluate) {
|
|
17
|
+ int count = 0;
|
|
18
|
+ E[] finalArray = mergeArrays(arrayToMerge);
|
|
19
|
+ for (E element : finalArray) {
|
|
20
|
+ if (element.equals(valueToEvaluate)) {
|
|
21
|
+ count++;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ return count;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public E[] mergeArrays(E[] arrayToMerge) {
|
|
28
|
+ ArrayList<E> arrayList = new ArrayList<>();
|
|
29
|
+ arrayList.addAll(Arrays.asList(inputArray));
|
|
30
|
+ arrayList.addAll(Arrays.asList(arrayToMerge));
|
|
31
|
+ return (E[]) arrayList.toArray();
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ public int getNumberOfOccurrences(E valueToEvaluate) {
|
|
35
|
+ int count = 0;
|
|
36
|
+ for (E element : inputArray) {
|
|
37
|
+ if (element == valueToEvaluate) {
|
|
38
|
+ count++;
|
|
39
|
+ }
|
|
40
|
+ }
|
|
41
|
+ return count;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ public E getMostCommonFromMerge(E[] arrayToMerge) {
|
|
45
|
+ E[] finalArray = this.mergeArrays(arrayToMerge);
|
|
46
|
+ this.inputArray = finalArray;
|
|
47
|
+
|
|
48
|
+ Map<E, Integer> hashMap = new HashMap<>();
|
|
49
|
+ for (E key : this.inputArray) {
|
|
50
|
+ if (hashMap.get(key) == null) {
|
|
51
|
+ hashMap.put(key, getNumberOfOccurrences(key));
|
|
52
|
+ }
|
|
53
|
+ }
|
|
54
|
+ int maxNumberOfOccurrences = Collections.max(hashMap.values());
|
|
55
|
+
|
|
56
|
+ E mostCommon = null;
|
|
57
|
+ for (Map.Entry<E, Integer> entry : hashMap.entrySet()) {
|
|
58
|
+ if (entry.getValue() == maxNumberOfOccurrences) {
|
|
59
|
+ mostCommon = entry.getKey();
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+ return mostCommon;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public E[] removeValue(E valueToRemove) {
|
|
66
|
+ int x = this.getNumberOfOccurrences(valueToRemove);
|
|
67
|
+ E[] reducedLengthArray = Arrays.copyOf(this.inputArray, this.inputArray.length - x);
|
|
68
|
+ for (int i = 0, j = 0; i < this.inputArray.length; i++) {
|
|
69
|
+ if (this.inputArray[i] != valueToRemove) {
|
|
70
|
+ reducedLengthArray[j] = this.inputArray[i];
|
|
71
|
+ j++;
|
|
72
|
+ }
|
|
73
|
+ }
|
|
74
|
+ return reducedLengthArray;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+
|
7
|
78
|
}
|