|
@@ -1,7 +1,75 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
|
2
|
+import java.util.*;
|
2
|
3
|
|
3
|
|
-/**
|
4
|
|
- * Created by leon on 3/6/18.
|
5
|
|
- */
|
6
|
|
-public class ArrayUtility {
|
7
|
|
-}
|
|
4
|
+public class ArrayUtility <T> {
|
|
5
|
+ private T[] inputArray;
|
|
6
|
+ private T[] mergeArray;
|
|
7
|
+ private Object valueToRemove;
|
|
8
|
+
|
|
9
|
+ public ArrayUtility(){}
|
|
10
|
+ public ArrayUtility(T[] inputArray) {
|
|
11
|
+ this.inputArray = inputArray;
|
|
12
|
+ }
|
|
13
|
+
|
|
14
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
15
|
+ mergeArray = (T[]) new Object[inputArray.length + arrayToMerge.length];
|
|
16
|
+ System.arraycopy(inputArray, 0,mergeArray, 0, inputArray.length);
|
|
17
|
+ System.arraycopy(arrayToMerge, 0, mergeArray, inputArray.length, arrayToMerge.length);
|
|
18
|
+ // System.out.println(mergeArray);
|
|
19
|
+
|
|
20
|
+ int numberOfOccurences = 0;
|
|
21
|
+ T mostCommon = mergeArray[0];
|
|
22
|
+ for (T element : mergeArray)
|
|
23
|
+ {
|
|
24
|
+ if (getNumberOfOccurrences(element) > numberOfOccurences)
|
|
25
|
+ {
|
|
26
|
+ mostCommon = element;
|
|
27
|
+ numberOfOccurences = getNumberOfOccurrences(element);
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ }
|
|
31
|
+ return mostCommon;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ public T[] removeValue (T valueToRemove){
|
|
36
|
+ Integer appearances = getNumberOfOccurrences(valueToRemove);
|
|
37
|
+ int i = 0;
|
|
38
|
+ //mergeArray = (T[]) new Object[inputArray.length-appearances];
|
|
39
|
+ mergeArray = Arrays.copyOf(inputArray, inputArray.length-appearances);
|
|
40
|
+ for (T element : inputArray) {
|
|
41
|
+ if (!element.equals(valueToRemove)) {
|
|
42
|
+ mergeArray[i] = element;
|
|
43
|
+ i++;
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+ return mergeArray;
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+ public Integer countDuplicatesInMerge (T[]arrayToMerge, T valueToEvaluate){
|
|
51
|
+ mergeArray = (T[]) new Object[inputArray.length + arrayToMerge.length];
|
|
52
|
+ System.arraycopy(inputArray, 0,mergeArray, 0, inputArray.length);
|
|
53
|
+ System.arraycopy(arrayToMerge, 0, mergeArray, inputArray.length, arrayToMerge.length);
|
|
54
|
+ Integer appearances = 0;
|
|
55
|
+ for (T element : mergeArray) {
|
|
56
|
+ if (element == valueToEvaluate) {
|
|
57
|
+ appearances++;
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ return appearances;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
65
|
+ // mergeArray = Arrays.copyOf(inputArray, inputArray.length);
|
|
66
|
+ Integer appearances = 0;
|
|
67
|
+ for (T element : inputArray) {
|
|
68
|
+ if (element == valueToEvaluate) {
|
|
69
|
+ appearances++;
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ return appearances;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ }//end of Class
|