-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
68 lines (55 loc) · 1.87 KB
/
ArrayUtility.java
File metadata and controls
68 lines (55 loc) · 1.87 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
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 <Object> {
Object[] objects;
public ArrayUtility(Object[] objects){
this.objects= objects;
}
public int countDuplicatesInMerge(Object[] arrayToMerge, Object valueToEvaluate) {
ArrayList<Object> added = new ArrayList<>(Arrays.asList(objects));
int numOfOccurences = 0;
Collections.addAll(added, arrayToMerge);
for (Object o : added) {
if (o == valueToEvaluate){
numOfOccurences++;
}
}
return numOfOccurences;
// int addLength= objects.length + arrayToMerge.length;
// Object[] merged = Arrays.copyOf(objects, addLength);
//
}
public Object getMostCommonFromMerge(Object[] arrayToMerge) {
ArrayList<Object> added = new ArrayList<>(Arrays.asList(objects));
Collections.addAll(added, arrayToMerge);
Object mostPop= null;
int most = Integer.MIN_VALUE;
for (Object object : added) {
if (getNumberOfOccurrences(object) > most){
mostPop = object;
most = getNumberOfOccurrences(object);
}
}
return mostPop;
}
public int getNumberOfOccurrences(Object valueToEvaluate) {
int numOfOccurences = 0;
for (Object obj : objects) {
if (obj == valueToEvaluate) {
numOfOccurences++;
}
}
return numOfOccurences;
}
public Object[] removeValue(Object valueToRemove) {
ArrayList<java.lang.Object> newArray = new ArrayList<>(Arrays.asList(objects));
newArray.removeAll(Collections.singleton(valueToRemove));
Object[] edit = (Object[]) newArray.toArray();
return edit;
}
}