|
@@ -1,7 +1,69 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.ArrayList;
|
|
4
|
+import java.util.Arrays;
|
|
5
|
+import java.util.Collections;
|
|
6
|
+
|
3
|
7
|
/**
|
4
|
8
|
* Created by leon on 3/6/18.
|
5
|
9
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
10
|
+public class ArrayUtility<T> {
|
|
11
|
+
|
|
12
|
+ private T [] myArray;
|
|
13
|
+ private ArrayList<T> myArrayList;
|
|
14
|
+ T[] resultArray;
|
|
15
|
+
|
|
16
|
+ public ArrayUtility(T[] myArray) {
|
|
17
|
+ this.myArray = myArray;
|
|
18
|
+ myArrayList = new ArrayList<>();
|
|
19
|
+ Collections.addAll(myArrayList,myArray);
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ public Integer countDuplicatesInMerge(T[]arrayToMerge, T valueToEvaluate){
|
|
23
|
+
|
|
24
|
+ Collections.addAll(myArrayList,arrayToMerge);
|
|
25
|
+ Integer count =0;
|
|
26
|
+ for(int i =0;i<myArrayList.size();i++){
|
|
27
|
+ if(myArrayList.get(i)==valueToEvaluate){
|
|
28
|
+ count++;
|
|
29
|
+ }
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ return count;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public T getMostCommonFromMerge(T[]arrayToMerge){
|
|
36
|
+
|
|
37
|
+ Collections.addAll(myArrayList,arrayToMerge);
|
|
38
|
+ T mostCommon =myArrayList.get(0);
|
|
39
|
+ for(int i=0;i<myArrayList.size();i++){
|
|
40
|
+ if(getNumberOfOccurrences(myArrayList.get(i))>getNumberOfOccurrences(mostCommon)){
|
|
41
|
+ mostCommon=myArrayList.get(i);
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ return mostCommon;
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate){
|
|
49
|
+ int count=0;
|
|
50
|
+ for(int i=0;i<myArrayList.size();i++){
|
|
51
|
+ if(myArrayList.get(i)==valueToEvaluate){
|
|
52
|
+ count++;
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+ return count;
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public T[] removeValue(T valueToRemove){
|
|
59
|
+ int resultArrayIndex=0;
|
|
60
|
+ resultArray = Arrays.copyOf(myArray,myArray.length-getNumberOfOccurrences(valueToRemove));
|
|
61
|
+ for(int i=0;i<myArray.length;i++){
|
|
62
|
+ if(!myArray[i].equals(valueToRemove)){
|
|
63
|
+ resultArray[resultArrayIndex]=myArray[i];
|
|
64
|
+ resultArrayIndex++;
|
|
65
|
+ }
|
|
66
|
+ }
|
|
67
|
+ return resultArray;
|
|
68
|
+ }
|
7
|
69
|
}
|