forked from catwood16/projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassScores.java
59 lines (48 loc) · 1.6 KB
/
ClassScores.java
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
import java.util.ArrayList;
import java.util.Arrays;
public class ClassScores {
public int[] findMode(int[] scores) {
Arrays.sort(scores);
int count = 0;
int highestcount = 0;
ArrayList<Integer> mode = new ArrayList<Integer>();
ArrayList<Integer> finalmode = new ArrayList<Integer>();
mode.add(0);
for (int i = 0; i < scores.length - 1; i++){
if (scores[i] == scores[i+1]){
count++;
mode.add(count);
}else{
count = 0;
mode.add(count);
}
}
int[] intmode = new int[mode.size()];
for (int i=0; i < intmode.length; i++){
intmode[i] = mode.get(i).intValue();
}
for (int i = 0; i < intmode.length; i++){
if (highestcount < intmode[i]){
highestcount = intmode[i];
}
}
for (int i = 0; i < intmode.length; i++){
if (highestcount == intmode[i]){
finalmode.add(scores[i]);
}
}
int[] intfinalmode = new int[finalmode.size()];
for (int i=0; i < intfinalmode.length; i++){
intfinalmode[i] = finalmode.get(i).intValue();
}
return intfinalmode;
}
public static void main(String [] args){
ClassScores tester = new ClassScores();
int [] scores = {92, 56, 14, 73, 22, 38, 93, 45, 55 };
tester.findMode(scores);
for (int i = 0; i < tester.findMode(scores).length; i++){
System.out.println(tester.findMode(scores)[i] + " ");
}
}
}