|
@@ -7,58 +7,67 @@ import java.util.Arrays;
|
7
|
7
|
/**
|
8
|
8
|
* Created by leon on 3/6/18.
|
9
|
9
|
*/
|
10
|
|
-public class ArrayUtility<E> {
|
|
10
|
+public class ArrayUtility<T> {
|
11
|
11
|
|
12
|
|
- private ArrayList<E> list;
|
13
|
|
- private E[] arr;
|
14
|
|
- private ArrayList<E> tempArr;
|
|
12
|
+ T[] list;
|
15
|
13
|
|
16
|
|
- public ArrayUtility( E[] arr) {
|
17
|
|
- list = new ArrayList<E>(Arrays.asList(arr));
|
18
|
|
- this.arr=arr;
|
|
14
|
+ public ArrayUtility(T[] list) {
|
|
15
|
+ this.list = list;
|
19
|
16
|
}
|
20
|
17
|
|
21
|
|
- public Integer getNumberOfOccurrences(E valueToEvaluate) {
|
22
|
|
- Integer counter = 0;
|
23
|
|
- for ( E el : list){
|
24
|
|
- if (el.equals(valueToEvaluate)){counter++;}
|
|
18
|
+ public int getNumberOfOccurrences(T element){
|
|
19
|
+ int count=0;
|
|
20
|
+ for(T el: list){
|
|
21
|
+ if(el.equals(element)){
|
|
22
|
+ count++;
|
|
23
|
+ }
|
25
|
24
|
}
|
26
|
|
- return counter;
|
|
25
|
+ return count;
|
27
|
26
|
}
|
28
|
27
|
|
29
|
|
- public void merge(E[] arrayToMerge) {
|
30
|
|
- for(E el : arrayToMerge){
|
31
|
|
- list.add(el);
|
|
28
|
+ public Object[] merge(T[] arr1){
|
|
29
|
+ Object[] mergeArr = new Object[list.length+arr1.length];
|
|
30
|
+ int counter=0;
|
|
31
|
+ for(T el : list ){
|
|
32
|
+ mergeArr[counter]=el;
|
|
33
|
+ counter++;
|
|
34
|
+ }
|
|
35
|
+ for(T el : arr1){
|
|
36
|
+ mergeArr[counter]=el;
|
|
37
|
+ counter++;
|
32
|
38
|
}
|
|
39
|
+ return mergeArr;
|
33
|
40
|
}
|
34
|
41
|
|
35
|
|
- public E getMostCommonFromMerge(E[] arrayToMerge) {
|
36
|
|
- merge(arrayToMerge);
|
37
|
|
- int frequencey = Integer.MIN_VALUE;
|
38
|
|
- E mostCommonElement =null;
|
39
|
|
- for (E el : list){
|
40
|
|
- if (getNumberOfOccurrences(el)>frequencey){
|
41
|
|
- frequencey = getNumberOfOccurrences(el);
|
42
|
|
- mostCommonElement = el;}
|
43
|
|
- }
|
44
|
|
- return mostCommonElement;
|
|
42
|
+ public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
|
|
43
|
+ ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
|
|
44
|
+ return arrayUtil.getNumberOfOccurrences(valueToEvaluate);
|
45
|
45
|
}
|
46
|
46
|
|
47
|
|
- public int countDuplicatesInMerge(E[] arrayToMerge, E valueToEvaluate) {
|
48
|
|
- merge(arrayToMerge);
|
49
|
|
- return getNumberOfOccurrences(valueToEvaluate);
|
|
47
|
+ public T getMostCommonFromMerge(T[] arrayToMerge) {
|
|
48
|
+ ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
|
|
49
|
+ int tempCount = 0;
|
|
50
|
+ T mostCommonObj = null;
|
|
51
|
+ for(Object el : arrayUtil.list){
|
|
52
|
+ if(getNumberOfOccurrences((T)el)>tempCount){
|
|
53
|
+ tempCount=getNumberOfOccurrences((T)el);
|
|
54
|
+ mostCommonObj=(T)el;
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+ return mostCommonObj;
|
50
|
58
|
}
|
51
|
59
|
|
52
|
|
- public E[] removeValue(E valueToRemove) {
|
53
|
|
- tempArr = new ArrayList<>();
|
54
|
|
- for(E el : list){
|
55
|
|
- if (el != valueToRemove){
|
56
|
|
- tempArr.add(el);
|
|
60
|
+ public T[] removeValue(T valueToRemove) {
|
|
61
|
+ ArrayList<T> tempArr = new ArrayList<>();
|
|
62
|
+ for(int i =0; i<list.length; i++){
|
|
63
|
+ if(!list[i].equals(valueToRemove)){
|
|
64
|
+ tempArr.add(list[i]);
|
57
|
65
|
}
|
58
|
66
|
}
|
59
|
67
|
int newLength = tempArr.size();
|
60
|
|
- arr = (E[]) Array.newInstance(arr.getClass().getComponentType(), newLength);
|
|
68
|
+ list = (T[]) Array.newInstance(list.getClass().getComponentType(), newLength);
|
61
|
69
|
|
62
|
|
- return tempArr.toArray(arr);
|
|
70
|
+ return tempArr.toArray(list);
|
63
|
71
|
}
|
|
72
|
+
|
64
|
73
|
}
|