-
Notifications
You must be signed in to change notification settings - Fork 0
/
FavouriteSinger.java
39 lines (30 loc) · 1.07 KB
/
FavouriteSinger.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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
// time = O(n), space = O(n)
public class FavouriteSingers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long[] singers = new long[n];
for (int i = 0; i < n; i++) {
singers[i] = scanner.nextLong();
}
// Create a HashMap to store the frequency of each singer
HashMap<Long, Integer> frequencyMap = new HashMap<>();
int maxFrequency = 0;
int maxFrequencyCount = 0;
for (long singer : singers) {
int newFrequency = frequencyMap.getOrDefault(singer, 0) + 1;
frequencyMap.put(singer, newFrequency);
if (newFrequency > maxFrequency) {
maxFrequency = newFrequency;
maxFrequencyCount = 1;
} else if (newFrequency == maxFrequency) {
maxFrequencyCount++;
}
}
System.out.println(maxFrequencyCount);
scanner.close();
}
}