-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
82 lines (73 loc) · 2.39 KB
/
ArrayUtility.java
File metadata and controls
82 lines (73 loc) · 2.39 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
74
75
76
77
78
79
80
81
82
package com.zipcodewilmington.arrayutility;
import java.util.*;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<SomeType> {
private SomeType[] array;
public ArrayUtility(SomeType[] array) {
this.array = array;
}
public List<SomeType> mergeArray(SomeType[] arrayToMerge){
List<SomeType> mergedArray = new ArrayList<>();
for (SomeType s : arrayToMerge) {
mergedArray.add(s);
}
for (SomeType s : this.array) {
mergedArray.add(s);
}
return mergedArray;
}
public Integer countDuplicatesInMerge(SomeType[] arrayToMerge, SomeType valueToEvaluate) {
List<SomeType> mergedArray = mergeArray(arrayToMerge);
int counter = 0;
for (SomeType something: mergedArray) {
if (something.equals(valueToEvaluate)){
counter++;
}
}
return counter;
}
public SomeType getMostCommonFromMerge(SomeType[] arrayToMerge) {
List<SomeType> mergedArray = mergeArray(arrayToMerge);
Map<SomeType, Integer> myMap = new HashMap<>();
for (SomeType s : mergedArray) {
if (myMap.containsKey(s)) {
myMap.put(s, myMap.get(s) + 1);
} else {
myMap.put(s, 1);
}
}
int mostCommon = 0;
SomeType theType = null;
for (Map.Entry<SomeType, Integer> a: myMap.entrySet()){
if (a.getValue() > mostCommon){
mostCommon = a.getValue();
theType = a.getKey();
}
}
return theType;
}
public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
int tracker = 0;
for (int i = 0; i < array.length; i++) {
if(valueToEvaluate.equals(array[i])) {
tracker++;
}
}
return tracker;
}
public SomeType[] removeValue(SomeType valueToRemove) {
ArrayList<SomeType> arrayList = new ArrayList<>();
for (SomeType element : array) {
if (!element.equals(valueToRemove)) {
arrayList.add(element);
}
}
SomeType[] newArray = (SomeType[]) java.lang.reflect.Array.newInstance(array[0].getClass(),array.length);
for (int i = 0; i < arrayList.size(); i++) {
newArray[i] = arrayList.get(i);
}
return newArray;
}
}