Please let me know why this is giving me wrong solution
public class RECNDNOS {
public static void main(String[] args) throws java.lang.Exception {
try {
InputReader in = new InputReader(System.in);
int t = in.nextInt();
while (t-- > 0) {
Map<Integer, Integer> keyAndCountMap = new HashMap<>();
int n = in.nextInt();
int a[] = Arrays.stream(in.nextLine().split(" ")).map(value -> Integer.parseInt(value)).mapToInt(x -> x).toArray();
int continousCount = 1;
for (int i = 0; i < a.length; i++) {
while (i < a.length - 1 && a[i] == a[i + 1]) {
continousCount += 1;
i++;
}
if (keyAndCountMap.containsKey(a[i])) {
int count = keyAndCountMap.get(a[i]);
keyAndCountMap.put(a[i], count + continousCount / 2 + continousCount % 2);
} else {
keyAndCountMap.put(a[i], continousCount / 2 + continousCount % 2);
}
continousCount = 1;
}
int maxKey = 0;
int maxCount = 0;
Iterator<Map.Entry<Integer, Integer>> iterator = keyAndCountMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, Integer> entry = iterator.next();
if (entry.getValue() > maxCount) {
maxKey = entry.getKey();
maxCount = entry.getValue();
}
}
System.out.println(maxKey);
}
} catch (Exception e) {
return;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}