|
@@ -1,7 +1,54 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.Arrays;
|
|
4
|
+import java.util.List;
|
|
5
|
+
|
3
|
6
|
/**
|
4
|
7
|
* Created by leon on 3/6/18.
|
5
|
8
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
9
|
+public class ArrayUtility<E> {
|
|
10
|
+
|
|
11
|
+ private E[] elementArr;
|
|
12
|
+
|
|
13
|
+ public ArrayUtility(E[] elementArr) {
|
|
14
|
+ this.elementArr = elementArr;
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ public Integer countDuplicatesInMerge(E[] arrayToMerge, E valueToEvaluate) {
|
|
18
|
+ Integer count = getNumberOfOccurrences(valueToEvaluate);
|
|
19
|
+ for (E e : arrayToMerge) {
|
|
20
|
+ if (valueToEvaluate == e) {
|
|
21
|
+ count++;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ return count;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public E getMostCommonFromMerge(E[] arrayToMerge) {
|
|
28
|
+ E mostCommon = elementArr[0];
|
|
29
|
+ Integer count = countDuplicatesInMerge(arrayToMerge, mostCommon);
|
|
30
|
+ for (E e : elementArr) {
|
|
31
|
+ Integer tempCount = countDuplicatesInMerge(arrayToMerge, e);
|
|
32
|
+ if (tempCount > count) {
|
|
33
|
+ mostCommon = e;
|
|
34
|
+ count = tempCount;
|
|
35
|
+ }
|
|
36
|
+ }
|
|
37
|
+ return mostCommon;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ public Integer getNumberOfOccurrences(E valueToEvaluate) {
|
|
41
|
+ Integer count = 0;
|
|
42
|
+ for (E e : elementArr) {
|
|
43
|
+ if (valueToEvaluate == e) {
|
|
44
|
+ count++;
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+ return count;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public E[] removeValue(E valueToRemove) {
|
|
51
|
+ while (Arrays.asList(elementArr).remove(valueToRemove)) {}
|
|
52
|
+ return elementArr;
|
|
53
|
+ }
|
7
|
54
|
}
|