-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG.java
49 lines (44 loc) · 1.32 KB
/
GFG.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
package data.structure;
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
class GFG
{
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt( br.readLine());
for( int i = 0 ; i < t ; t++){
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
int n = Integer.parseInt(br.readLine());
String str[] = br.readLine().split(" ");
for(String s : str)
{
int key = Integer.parseInt(s);
if(hm.containsKey(key) == true)
{
hm.replace(key, hm.get(key), hm.get(key)+1);
}
else
hm.put(key, 1);
}
int max = 0;
int value = 0;
for(Entry<Integer,Integer> e : hm.entrySet())
{
if(e.getValue()>max) {
max = e.getValue();
value = e.getKey();
}
}
if(max > n/2)
{
System.out.println(value);
}
else
{
System.out.println("-1");
}
}
}
}