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