-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
60 lines (48 loc) · 1.75 KB
/
ArrayUtility.java
File metadata and controls
60 lines (48 loc) · 1.75 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
package com.zipcodewilmington.arrayutility;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<T> {
T[] inputArray;
public ArrayUtility(T[] inputArray) {
this.inputArray = inputArray;
}
public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
ArrayList<T> input = new ArrayList<>(Arrays.asList(inputArray));
ArrayList<T> merge = new ArrayList<>(Arrays.asList(arrayToMerge));
input.addAll(merge);
return Collections.frequency(input, valueToEvaluate);
}
public T getMostCommonFromMerge(T[] arrayToMerge) {
ArrayList<T> input = new ArrayList<>(Arrays.asList(inputArray));
ArrayList<T> merge = new ArrayList<>(Arrays.asList(arrayToMerge));
T mostCommon = null;
input.addAll(merge);
Integer highestCount = 0;
for(T element : input) {
Integer tempCount = Collections.frequency(input, element);
if(tempCount > highestCount) {
highestCount = tempCount;
mostCommon = element;
}
}
return mostCommon;
}
public Integer getNumberOfOccurrences(T valueToEvaluate) {
ArrayList<T> input = new ArrayList<>(Arrays.asList(inputArray));
return Collections.frequency(input, valueToEvaluate);
}
public Object[] removeValue(T valueToRemove) {
ArrayList<T> input = new ArrayList<>(Arrays.asList(inputArray));
ArrayList<T> newList = new ArrayList<>();
for(T element : input) {
if(!element.equals(valueToRemove)) {
newList.add(element);
}
}
return newList.toArray(new Object[0]);
}
}