-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
73 lines (59 loc) · 1.96 KB
/
ArrayUtility.java
File metadata and controls
73 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.zipcodewilmington.arrayutility;
import java.lang.reflect.Array;
import java.util.Arrays;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<E>{
E[] inputArray;
public ArrayUtility(E[] inputArray){
this.inputArray = inputArray;
}
public Integer countDuplicatesInMerge(E[] arrayToMerge, E valueToEvaluate ){
E[] newArray = mergeArrays(arrayToMerge);
return countDuplicates(valueToEvaluate,newArray);
}
private Integer countDuplicates(E valueToEvaluate,E[] newArray){
Integer counter = 0;
for(E element : newArray){
if(element.equals(valueToEvaluate)){
counter++;
}
}
return counter;
}
public E getMostCommonFromMerge(E[] arrayToMerge){
Integer max = 0;
E value = null;
E[] newArray = mergeArrays(arrayToMerge);
for(E element : newArray){
Integer count = countDuplicates(element,inputArray);
if(count>max){
max = count;
value = element;
}
}
return value;
}
public Integer getNumberOfOccurrences(E valueToEvaluate){
return countDuplicates(valueToEvaluate,this.inputArray);
}
public E[] removeValue(E valueToRemove) {
Integer countRemove = getNumberOfOccurrences(valueToRemove);
Integer counter = 0;
E[] newArray = Arrays.copyOf(this.inputArray,this.inputArray.length-countRemove);
for(E element : this.inputArray){
if(!element.equals(valueToRemove)){
newArray[counter++]=element;
}
}
return newArray;
}
private E[] mergeArrays(E[] arrayToMerge){
E[] newArray = Arrays.copyOf(this.inputArray,this.inputArray.length+arrayToMerge.length);
for(int i = 0; i< arrayToMerge.length;i++){
newArray[i+ this.inputArray.length] = arrayToMerge[i];
}
return newArray;
}
}