|
@@ -1,7 +1,72 @@
|
1
|
1
|
package com.zipcodewilmington.arrayutility;
|
2
|
2
|
|
|
3
|
+import java.util.*;
|
|
4
|
+
|
3
|
5
|
/**
|
4
|
6
|
* Created by leon on 3/6/18.
|
5
|
7
|
*/
|
6
|
|
-public class ArrayUtility {
|
|
8
|
+public class ArrayUtility<T> {
|
|
9
|
+
|
|
10
|
+ protected T[] inputArray;
|
|
11
|
+
|
|
12
|
+ public ArrayUtility(T[] inputArray) {
|
|
13
|
+ this.inputArray = inputArray;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
17
|
+ ArrayList<T> mergedArrays = new ArrayList<T>(Arrays.asList(inputArray));
|
|
18
|
+ mergedArrays.addAll(Arrays.asList(arrayToMerge));
|
|
19
|
+ Integer occurred = count(mergedArrays, valueToEvaluate);
|
|
20
|
+ return occurred;
|
|
21
|
+ }
|
|
22
|
+
|
|
23
|
+ public T getMostCommonFromMerge(T[] arrayToMerge){
|
|
24
|
+ ArrayList<T> mergedArrays = new ArrayList<T>(Arrays.asList(arrayToMerge));
|
|
25
|
+ mergedArrays.addAll(Arrays.asList(inputArray));
|
|
26
|
+ T common = null;
|
|
27
|
+ for(int x = 0; x<mergedArrays.size(); x++) {
|
|
28
|
+ for(int y = 0; y< mergedArrays.size(); y++){
|
|
29
|
+ if(count(mergedArrays, mergedArrays.get(x))> count(mergedArrays, mergedArrays.get(y))){
|
|
30
|
+ common = mergedArrays.get(x);
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ return common;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ public Integer getNumberOfOccurrences(T valueToEvaluate){
|
|
38
|
+ ArrayList<T> obArray = new ArrayList<T>(Arrays.asList(inputArray));
|
|
39
|
+ return count(obArray, valueToEvaluate);
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ public T[] removeValue(T valueToRemove){
|
|
43
|
+ ArrayList<T> obArray = new ArrayList<T>(Arrays.asList(inputArray));
|
|
44
|
+ int counter = count(obArray, valueToRemove);
|
|
45
|
+ T[] shorten = Arrays.copyOf(inputArray, inputArray.length-counter);
|
|
46
|
+ for(int i = 0; i<obArray.size(); i++){
|
|
47
|
+ if(valueToRemove.equals(obArray.get(i)) ){
|
|
48
|
+ obArray.remove(obArray.get(i));
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+ return obArray.toArray(shorten);
|
|
52
|
+
|
|
53
|
+ }
|
|
54
|
+ /*public void removeStudent(String firstName, String lastName) {
|
|
55
|
+
|
|
56
|
+ if (students[p] == null) {
|
|
57
|
+ students[p] = this.students[p + 1];
|
|
58
|
+ students[p + 1] = null;
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+ }*/
|
|
62
|
+
|
|
63
|
+ public Integer count(ArrayList<T> mergedArrays, T valueToEvaluate) {
|
|
64
|
+ Integer count = 0;
|
|
65
|
+ for(int i = 0; i<mergedArrays.size();i++) {
|
|
66
|
+ if (mergedArrays.get(i).equals(valueToEvaluate)) {
|
|
67
|
+ count++;
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ return count;
|
|
71
|
+ }
|
7
|
72
|
}
|