-
Notifications
You must be signed in to change notification settings - Fork 0
/
conformity.java
49 lines (43 loc) · 1.44 KB
/
conformity.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
import java.util.*;
public class conformity{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//no of frosh
int num = sc.nextInt();
sc.nextLine(); //dummy read
HashMap<String, Integer> coursesHash = new HashMap<String, Integer>();
for (int i = 0; i < num; i++) {
String strCourses = sc.nextLine();
String[] arrayCourses = strCourses.split(" ");
Arrays.sort(arrayCourses);
String keyCourses = "";
for (String course : arrayCourses) {
keyCourses = keyCourses.concat(course);
}
// check if combination of courses already in coursesHash
if (coursesHash.containsKey(keyCourses)) {
int val = coursesHash.get(keyCourses);
coursesHash.replace(keyCourses, ++val);
}
// else add new combination to coursesHash
else {
coursesHash.put(keyCourses, 1);
}
}
Set<String> allKeys = coursesHash.keySet();
int maxVal = 0;
int count = 0;
for (String key : allKeys) {
//the number of students with the courses combination of "key"
int numCombi = coursesHash.get(key);
// if number of max combinations is more than others, count = num of students taking
if (numCombi > maxVal){
maxVal = numCombi;
count = numCombi;
}
// if number of max combinations is the same, increment count by num of students taking
else if (numCombi == maxVal) count += coursesHash.get(key);
}
System.out.println(count);
}
}