|
@@ -1,7 +1,79 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.lang.reflect.Array;
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+import java.util.Arrays;
|
|
6
|
+import java.util.List;
|
|
7
|
+
|
3
|
8
|
/**
|
4
|
9
|
* Created by leon on 3/6/18.
|
5
|
10
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
11
|
+public class ArrayUtility <T> {
|
|
12
|
+ private T[] inputArray;
|
|
13
|
+
|
|
14
|
+ ArrayUtility(T[] inputArray){
|
|
15
|
+ this.inputArray = inputArray;
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ public Integer countDuplicatesInMerge(T [] arrayToMerge, T valueToEvaluate) {
|
|
19
|
+ Integer count = 0;
|
|
20
|
+ T[] newArray = merge(inputArray,arrayToMerge);
|
|
21
|
+
|
|
22
|
+ for (int i = 0; i < newArray.length ;i++) {
|
|
23
|
+ if (newArray[i] == valueToEvaluate){
|
|
24
|
+ count++;
|
|
25
|
+ }
|
|
26
|
+ }
|
|
27
|
+ return count;
|
|
28
|
+
|
|
29
|
+ }
|
|
30
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
31
|
+ Object[] newArray = merge(inputArray,arrayToMerge);
|
|
32
|
+ T mostCommonValue = (T) newArray[0];
|
|
33
|
+ Object temp;
|
|
34
|
+ int count = 1, tempCount;
|
|
35
|
+ int i;
|
|
36
|
+ int j;
|
|
37
|
+ for (i = 0; i <newArray.length-1 ; i++) {
|
|
38
|
+ temp = newArray[i];
|
|
39
|
+ tempCount = 0;
|
|
40
|
+ for (j = 0; j <newArray.length ; j++) {
|
|
41
|
+ if (temp == newArray[j])
|
|
42
|
+ tempCount++;
|
|
43
|
+ }
|
|
44
|
+ if (tempCount > count)
|
|
45
|
+ {
|
|
46
|
+ mostCommonValue = (T) temp;
|
|
47
|
+ count = tempCount;
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ return mostCommonValue;
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate) {
|
|
54
|
+ Integer count = 0;
|
|
55
|
+ for (int i = 0; i < inputArray.length ;i++) {
|
|
56
|
+ if (inputArray[i] == valueToEvaluate){
|
|
57
|
+ count++;
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+ return count;
|
|
61
|
+
|
|
62
|
+ }
|
|
63
|
+ public T[] removeValue(T valueToRemove) {
|
|
64
|
+ List<T> list = new ArrayList<>();
|
|
65
|
+ for(T element : inputArray){
|
|
66
|
+ if (!element.equals(valueToRemove)){
|
|
67
|
+ list.add(element);
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ return list.toArray((T[])Array.newInstance(inputArray.getClass().getComponentType(),list.size()));
|
|
71
|
+
|
|
72
|
+ }
|
|
73
|
+ public T[] merge(T[] inputArray, T[] arrayToMerge){
|
|
74
|
+ T[] newArr = (T[]) new Object[arrayToMerge.length + inputArray.length];
|
|
75
|
+ System.arraycopy(inputArray,0,newArr,0,inputArray.length);
|
|
76
|
+ System.arraycopy(arrayToMerge,0,newArr,inputArray.length,arrayToMerge.length);
|
|
77
|
+ return newArr;
|
|
78
|
+ }
|
7
|
79
|
}
|