-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
74 lines (65 loc) · 1.73 KB
/
ArrayUtility.java
File metadata and controls
74 lines (65 loc) · 1.73 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
package com.zipcodewilmington.arrayutility;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<E> {
E[] inputArray;
public ArrayUtility(E[] inputArray){
this.inputArray=inputArray;
}
public Integer getNumberOfOccurrences(E valueToEvaluate){
int count =0;
for (E element:inputArray
) {
if(element==valueToEvaluate) {
count++;
}
}
return count;
}
public E[] removeValue(E valueToRemove) {
int numberOfOccurrence = getNumberOfOccurrences(valueToRemove);
E[] newArray = Arrays.copyOf(inputArray, inputArray.length - numberOfOccurrence);
int j = 0;
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] != valueToRemove) {
newArray[j] = inputArray[i];
j++;
}
}
return newArray;
}
public Integer countDuplicatesInMerge(E[] arrayToMerge,E valueToEvaluate){
int count=0;
int count1=0;
for(E element:arrayToMerge){
if(element ==valueToEvaluate){
count+=1;
}
}
for(E elements:inputArray){
if(elements==valueToEvaluate){
count1+=1;
}
}
return count+count1;
}
public E getMostCommonFromMerge(E[] arrayToMerge){
List<E> input= new ArrayList<E>(Arrays.asList(inputArray));
// List<E> merge= new ArrayList<E>(Arrays.asList(arrayToMerge));
input.addAll(Arrays.asList(arrayToMerge));
E mostCommon=null;
int count=0;
for (E element:input
) {
if(getNumberOfOccurrences(element)>count){
mostCommon=element;
count=getNumberOfOccurrences(element);
}
}
return mostCommon;
}
}