|
@@ -1,7 +1,91 @@
|
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<shivam> {
|
|
10
|
+
|
|
11
|
+ private shivam[] inputArray;
|
|
12
|
+
|
|
13
|
+ public ArrayUtility(shivam[] inputArray) {
|
|
14
|
+
|
|
15
|
+ this.inputArray = inputArray;
|
|
16
|
+
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ public shivam[] merge(shivam[] inputArray, shivam[] arrayToMerge) {
|
|
20
|
+ shivam[] newArr = (shivam[]) new Object[arrayToMerge.length + inputArray.length];
|
|
21
|
+ System.arraycopy(inputArray, 0, newArr, 0, inputArray.length);
|
|
22
|
+ System.arraycopy(arrayToMerge, 0, newArr, inputArray.length, arrayToMerge.length);
|
|
23
|
+
|
|
24
|
+ return newArr;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public Integer countDuplicatesInMerge(shivam[] arrayToMerge, shivam valueToEvaluate) {
|
|
28
|
+ int count = 0;
|
|
29
|
+ shivam[] newArr = merge(arrayToMerge, inputArray);
|
|
30
|
+
|
|
31
|
+ for (shivam s : newArr) {
|
|
32
|
+ if (s.equals(valueToEvaluate)) {
|
|
33
|
+
|
|
34
|
+ count++;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ }
|
|
38
|
+ return count;
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ public shivam getMostCommonFromMerge(shivam[] arrayToMerge) {
|
|
42
|
+ shivam[] newArr = merge(inputArray, arrayToMerge);
|
|
43
|
+ shivam common = newArr[0];
|
|
44
|
+ int count = 1;
|
|
45
|
+ shivam temp;
|
|
46
|
+ int tempCount = 0;
|
|
47
|
+
|
|
48
|
+ for (int i = 0; i < (newArr.length - 1); i++) {
|
|
49
|
+ temp = newArr[i];
|
|
50
|
+
|
|
51
|
+ for (int j = 1; j < newArr.length; j++) {
|
|
52
|
+ if (temp == newArr[j])
|
|
53
|
+ tempCount++;
|
|
54
|
+ }
|
|
55
|
+ if (tempCount > count) {
|
|
56
|
+ common = temp;
|
|
57
|
+ count = tempCount;
|
|
58
|
+ }
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ return common;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ public Integer getNumberOfOccurrences(shivam valueToEvaluate) {
|
|
65
|
+ int count = 0;
|
|
66
|
+
|
|
67
|
+ for (shivam s : inputArray) {
|
|
68
|
+ if (s.equals(valueToEvaluate)) {
|
|
69
|
+ count++;
|
|
70
|
+
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ return count;
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ public shivam[] removeValue(shivam valueToRemove) {
|
|
78
|
+ Integer removed = getNumberOfOccurrences(valueToRemove);
|
|
79
|
+ shivam[] array = (shivam[]) Array.newInstance(inputArray.getClass().getComponentType(), inputArray.length-removed);
|
|
80
|
+ int count = 0;
|
|
81
|
+ for (int i = 0; i < inputArray.length; i++) {
|
|
82
|
+ if (!inputArray[i].equals(valueToRemove)) {
|
|
83
|
+ array[count] = inputArray[i];
|
|
84
|
+ count++;
|
|
85
|
+
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ }
|
|
89
|
+ return array;
|
|
90
|
+ }
|
7
|
91
|
}
|