-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArrayUtility.java
More file actions
90 lines (74 loc) · 2.66 KB
/
ArrayUtility.java
File metadata and controls
90 lines (74 loc) · 2.66 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
package com.dtcc.exams.part2;
import java.sql.Array;
import java.util.*;
public class ArrayUtility<ArrayDataType> {
//ArrayUtility<Integer> arrayUtility = new ArrayUtility<>(inputArray);
public Object[] inputArray;
public ArrayUtility(ArrayDataType[] inputArray){
this.inputArray=inputArray;
}
public Integer countDuplicatesInMerge(ArrayDataType[] arrayToMerge, ArrayDataType valueToEvaluate){
int count=0;
for(int i=0;i<inputArray.length;i++){
if(inputArray[i]==valueToEvaluate){
count++;
}
}
for(int i=0;i<arrayToMerge.length;i++){
if(arrayToMerge[i]==valueToEvaluate){
count++;
}
}
return count;
}
public ArrayDataType getMostCommonFromMerge(ArrayDataType[] arrayToMerge, ArrayDataType valueToEvaluate){
Map<ArrayDataType,Integer> map=new HashMap<ArrayDataType,Integer>();
for(int i=0;i<arrayToMerge.length;i++){ //loop for ArrayToMerge
if(map.containsKey(arrayToMerge[i])){
int value= map.get(arrayToMerge[i]);
map.put(arrayToMerge[i],value+1);
}
else{
map.put(arrayToMerge[i],1);
}
}
for(int i=0;i<inputArray.length;i++){ //loop for inputarray
if(map.containsKey(inputArray[i])){
int value= map.get(inputArray[i]);
map.put((ArrayDataType) inputArray[i],value+1);
}
else{
map.put((ArrayDataType) inputArray[i],1);
}
}
int maxValueInMap=(Collections.max(map.values()));
ArrayDataType mostCommon=null;
for (Map.Entry<ArrayDataType,Integer> entry : map.entrySet()) {
if (entry.getValue()==maxValueInMap) {
mostCommon=entry.getKey();
}
}
// return valueToEvaluate;
return mostCommon;
}
public ArrayDataType[] removeValue(ArrayDataType valueToRemove){
List<ArrayDataType> list=new ArrayList<ArrayDataType>();
for(int i=0;i<inputArray.length;i++){
if(inputArray[i]!=valueToRemove){
list.add((ArrayDataType) inputArray[i]);
}
}
ArrayDataType[] arrayToSend= (ArrayDataType[]) new Object[list.size()];
arrayToSend=(ArrayDataType[]) list.toArray(arrayToSend);
return arrayToSend;
}
public Integer getNumberOfOccurrences(ArrayDataType valueToEvaluate ){
int count=0;
for(int i=0;i<inputArray.length;i++){
if(inputArray[i]==valueToEvaluate){
count++;
}
}
return count;
}
}