-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
94 lines (65 loc) · 2.07 KB
/
ArrayUtility.java
File metadata and controls
94 lines (65 loc) · 2.07 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
83
84
85
86
87
88
89
90
91
92
93
94
package com.zipcodewilmington.arrayutility;
import com.sun.org.apache.xpath.internal.operations.String;
import java.io.OptionalDataException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Created by leon on 3/6/18.
*/
// created a generic class
public class ArrayUtility<T> {// created a generic class
private T[] inputArray;
public ArrayUtility(T[] inputArray) {
this.inputArray = inputArray;
}
public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
Integer counter = 0;
for (int i = 0; i < inputArray.length; i++){
if (inputArray[i].equals(valueToEvaluate)) {
counter++;
}
}
for (int j = 0; j < arrayToMerge.length; j++) {
if (arrayToMerge[j].equals(valueToEvaluate)) {
counter++;
}
}
return counter;
}
public T getMostCommonFromMerge (T[] arrayToMerge) {
T mostCommon = null;
for (int i = 0; i < inputArray.length; i++) {
for (int j = 0; j < arrayToMerge.length;j++){
if(inputArray[i] == arrayToMerge[j]){
mostCommon = inputArray[i];
}
}
}
return mostCommon;
}
public Integer getNumberOfOccurrences( T valueToEvaluate) {
Integer counter = 0;
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i].equals(valueToEvaluate)){
counter++;
}
}
return counter;
}
public T[] removeValue( T valueToRemove) {
T[] newArray;
Integer counter = 0;
Integer remove = getNumberOfOccurrences(valueToRemove);
Integer newSize = inputArray.length - remove;
newArray = Arrays.copyOf(inputArray, newSize);
for (int i = 0; i < inputArray.length; i++) {
if(inputArray[i] != valueToRemove){
newArray[counter] = inputArray[i];
counter++;
}
}
return newArray;
}
}