Здравейте,
имам проблем със следната задача:
Most Frequent
Write a program that finds the most frequent number in an array of N elements.
Input
- On the first line you will receive the number N
- On the next N lines the numbers of the array will be given
Output
- Print the most frequent number and how many time it is repeated
- Output should be
REPEATING_NUMBER (REPEATED_TIMES times)
- Output should be
Constraints
- 1 <= N <= 1024
- 0 <= each number in the array <= 10000
- There will be only one most frequent number
Кода минава с 80 точки и се чупи на два от тестовете.
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
int[] numbers = new int[input];
int counter = 1;
int check = 0;
int result = 0;
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
Arrays.sort(numbers);
for (int i = 0; i < numbers.length - 1; i++) {
if (numbers[i] == numbers[i + 1]){
counter++;
}else {
if (counter > check) {
check = counter;
result = numbers[i];
}
counter = 1;
}
}
System.out.println(result + " (" + check + " times)");
Test case #11: Wrong Answer [0.2 s, 31.1 MB]
Your output (clipped)
0 (0 times)