|
@@ -1,7 +1,81 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+
|
3
|
6
|
/**
|
4
|
7
|
* Created by leon on 3/6/18.
|
5
|
8
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
9
|
+public class ArrayUtility<T> {
|
|
10
|
+ private T[] genericArray;
|
|
11
|
+
|
|
12
|
+ public ArrayUtility(T[] inputArray){
|
|
13
|
+ this.genericArray = inputArray;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate){
|
|
17
|
+ T[] mergedArray = mergeArrays(genericArray, arrayToMerge);
|
|
18
|
+ Integer howManyTimesThisValueShowsUp = 0;
|
|
19
|
+ for(T value : mergedArray){
|
|
20
|
+ if(value == valueToEvaluate){
|
|
21
|
+ howManyTimesThisValueShowsUp++;
|
|
22
|
+ }
|
|
23
|
+ }
|
|
24
|
+ return howManyTimesThisValueShowsUp;
|
|
25
|
+ }
|
|
26
|
+
|
|
27
|
+ public T getMostCommonFromMerge(T[] arrayToMerge){
|
|
28
|
+ T[] mergedArray = mergeArrays(genericArray, arrayToMerge);
|
|
29
|
+ int count = 0;
|
|
30
|
+ int tempCount;
|
|
31
|
+ T mostFreq = mergedArray[0];
|
|
32
|
+ T currentObjectObserved;
|
|
33
|
+ //search for the most freq
|
|
34
|
+ for(int i = 0; i < mergedArray.length; i++){
|
|
35
|
+ currentObjectObserved = mergedArray[i];
|
|
36
|
+ tempCount = 1;
|
|
37
|
+ for(int j = 1; j <mergedArray.length; j++){
|
|
38
|
+ if(currentObjectObserved == mergedArray[j]){
|
|
39
|
+ tempCount++;
|
|
40
|
+ }
|
|
41
|
+ }
|
|
42
|
+ if(tempCount > count){
|
|
43
|
+ mostFreq = currentObjectObserved;
|
|
44
|
+ count = tempCount;
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+ return mostFreq;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate){
|
|
51
|
+ Integer howManyTimesThisValueShowsUp = 0;
|
|
52
|
+ for(T value : genericArray){
|
|
53
|
+ if(value == valueToEvaluate){
|
|
54
|
+ howManyTimesThisValueShowsUp++;
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+ return howManyTimesThisValueShowsUp;
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public T[] removeValue(T valueToRemove){
|
|
61
|
+ Integer numberOfOccurrences = getNumberOfOccurrences(valueToRemove);
|
|
62
|
+ T[] returnArray = Arrays.copyOf(genericArray, genericArray.length-numberOfOccurrences);
|
|
63
|
+ int indexCount = 0;
|
|
64
|
+ for(T value : genericArray){
|
|
65
|
+ if(!value.equals(valueToRemove)){
|
|
66
|
+ returnArray[indexCount] = value;
|
|
67
|
+ indexCount++;
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ return returnArray;
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+ public T[] mergeArrays(T[] originalArray, T[] arrayToMerge){
|
|
75
|
+ ArrayList<T> merged = new ArrayList<>();
|
|
76
|
+ merged.addAll(Arrays.asList(originalArray));
|
|
77
|
+ merged.addAll(Arrays.asList(arrayToMerge));
|
|
78
|
+ T[] mergedArray = (T[]) merged.toArray();
|
|
79
|
+ return mergedArray;
|
|
80
|
+ }
|
7
|
81
|
}
|