-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
58 lines (50 loc) · 1.6 KB
/
ArrayUtility.java
File metadata and controls
58 lines (50 loc) · 1.6 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
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<SomeType> {
SomeType[] array;
public ArrayUtility(SomeType[] input){
this.array = input;
}
public SomeType[] removeValue(SomeType valueToRemove) {
ArrayList<SomeType> removeArray = new ArrayList<>();
for(SomeType someType : array) {
if(someType != valueToRemove) {
removeArray.add(someType);
}
}
return removeArray.toArray(Arrays.copyOf(array, removeArray.size()));
}
public Integer countDuplicatesInMerge(SomeType[] arrayToMerge, SomeType valueToEvaluate) {
int count = 0;
for(SomeType someType : arrayToMerge){
if(someType == valueToEvaluate) {
count++;
}
}
for(SomeType someType : array) {
if(someType == valueToEvaluate){
count++;
}
}
return count;
}
public SomeType getMostCommonFromMerge(SomeType[] arrayToMerge) {
int count = 0;
SomeType mostCommmon = null;
for (SomeType someType : arrayToMerge) {
if(getNumberOfOccurrences(someType) > count) {
count =getNumberOfOccurrences(someType);
mostCommmon = someType;
}
}
return mostCommmon;
}
public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
return Collections.frequency(Arrays.asList(array), valueToEvaluate);
}
}