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