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