-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
55 lines (46 loc) · 1.74 KB
/
ArrayUtility.java
File metadata and controls
55 lines (46 loc) · 1.74 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
package com.zipcodewilmington.arrayutility;
import java.util.*;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<T> {
T [] currentArray;
public ArrayUtility(T[] inputArr){
this.currentArray = inputArr;
}
public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
ArrayList<T> mergedList = combine(arrayToMerge);
this.currentArray = (T[]) mergedList.toArray();
Integer count = getNumberOfOccurrences(valueToEvaluate);
return count;
}
public T getMostCommonFromMerge(T[] arrayToMerge) {
ArrayList<T> mergedList = combine(arrayToMerge);
T mostCommon = null;
int mostCount = 0;
for(T element : mergedList) {
Integer currentCount = getNumberOfOccurrences(element);
if(currentCount > mostCount){
mostCount = currentCount;
mostCommon = element;
}
}
return mostCommon;
}
public Integer getNumberOfOccurrences(T valueToEvaluate) {
ArrayList<T> currentList = new ArrayList<>(Arrays.asList(currentArray));
Integer count = Collections.frequency(currentList,valueToEvaluate);
return count;
}
public T[] removeValue(T valueToRemove) {
ArrayList<T> currentList = new ArrayList<>(Arrays.asList(currentArray));
currentList.removeIf(e -> e.equals(valueToRemove));
return (T[]) currentList.toArray();
}
private ArrayList<T> combine(T[] arrayToCombine) {
ArrayList<T> arrToMergeAsList = new ArrayList<>(Arrays.asList(arrayToCombine));
ArrayList<T> currentList = new ArrayList<>(Arrays.asList(currentArray));
currentList.addAll(arrToMergeAsList);
return currentList;
}
}