Skip to content

Instantly share code, notes, and snippets.

@longnguyencse
Created April 9, 2015 08:25
Show Gist options
  • Save longnguyencse/856045b40c9a551c89e0 to your computer and use it in GitHub Desktop.
Save longnguyencse/856045b40c9a551c89e0 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class type {
static int getMaxOfArray(int a[]) {
int max = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > max)
max = a[i];
}
return max;
}
public static void main(String args[]) throws NumberFormatException,
IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
int a[], b[], result, max;
int n = Integer.parseInt(in.readLine());
/* define array b to update result */
a = new int[n];
String[] num = new String[n];
num = in.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(num[i]);
}
b = new int[getMaxOfArray(a)];
/* update result */
for (int i = 0; i < n; i++) {
b[a[i]-1]++;
}
max = 0;
result = 0;
/* find max of array b and index of max*/
for (int i = 0; i < b.length; i++) {
if (b[i] == max) {
if (i+1 < result)
result = i+1;
} else if (b[i] > max) {
max = b[i];
result = i+1;
}
}
/*print result*/
System.out.println(result + " " + b[result-1]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment