-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrequency Game
58 lines (47 loc) · 1.59 KB
/
Frequency Game
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
//{ Driver Code Starts
// Initial Template for Java
import java.util.*;
import java.io.*;
// Position this line where user code will be pasted.
// Driver class with main function
class GFG {
// Main function
public static void main(String[] args)throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
// Iterating over testcases
while (t-- > 0) {
int n = Integer.parseInt(in.readLine());
int arr[] = new int[n];
String s[]=in.readLine().trim().split(" ");
for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(s[i]);
Solution obj = new Solution();
System.out.println(obj.LargButMinFreq(arr, n));
}
}
}
// } Driver Code Ends
// User function Template for Java
// Helper class to find largest number with minimum frequency
class Solution {
// Function to find largest number with minimum frequency
public static int LargButMinFreq(int arr[], int n) {
// Your code here
HashMap<Integer,Integer> mm = new HashMap<>();
for(int i=0;i<n;i++){
mm.putIfAbsent(arr[i],0);
mm.put(arr[i],mm.get(arr[i])+1);
}
int ans=0,f=Integer.MAX_VALUE;
for(Map.Entry<Integer,Integer> x:mm.entrySet()){
if(x.getValue()<f){
f=x.getValue();
ans=x.getKey();
}
else if(x.getValue()==f){
ans=Math.max(ans,x.getKey());
}
}
return ans;
}
}