-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
78 lines (69 loc) · 2.29 KB
/
ArrayUtility.java
File metadata and controls
78 lines (69 loc) · 2.29 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
package com.zipcodewilmington.arrayutility;
import com.sun.deploy.util.ArrayUtil;
import com.sun.tools.javac.util.ArrayUtils;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<SomeType> {
SomeType[] intArray;
public ArrayUtility(SomeType[] intArray){
this.intArray = intArray;
}
public Integer countDuplicatesInMerge(SomeType[] arrayToMerge, SomeType valueToEvaluate) {
Integer originalCounter = 0;
Integer mergeCounter = 0;
for(SomeType type : intArray){
if(type == valueToEvaluate)
originalCounter++;
}
for(SomeType type : arrayToMerge){
if(type == valueToEvaluate)
mergeCounter++;
}
Integer result = originalCounter + mergeCounter;
return result;
}
public SomeType getMostCommonFromMerge(SomeType[] arrayToMerge) {
Stream<SomeType> stream = Stream.concat(Arrays.stream(intArray), Arrays.stream(arrayToMerge));
SomeType[] work = (SomeType[]) stream.toArray();
SomeType element = null;
int counter = 0;
for (int i = 0; i < work.length; i++) {
SomeType currentType = work[i];
int currentTypeCount = 0;
for(int j = 0; j < work.length; j++){
if (work[j] == currentType){
currentTypeCount++;
}
if(currentTypeCount > counter){
element = currentType;
counter = currentTypeCount;
}
}
}
return element;
}
public Integer getNumberOfOccurrences(SomeType valueToEvaluate) {
int counter = 0;
for (int j = 0; j < intArray.length; j++) {
if(intArray[j] == valueToEvaluate){
counter++;
}
}
return counter;
}
public SomeType[] removeValue(SomeType valueToRemove) {
List<SomeType> list = new ArrayList<>();
for(SomeType type : intArray){
if(type != valueToRemove)
list.add(type);
}
SomeType[] result = list.toArray(Arrays.copyOf(intArray,list.size()));
return result;
}
}