Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Array;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int element = Integer.MIN_VALUE, max_count = 1, count =1;
- //intial array
- int a = Integer.parseInt(scanner.nextLine());
- int[] arr = new int[a];
- for (int i = 0; i < arr.length; i++) {
- a = Integer.parseInt(scanner.nextLine());
- arr[i] += a;
- }
- //sort array in the ascending Order
- Arrays.sort(arr);
- //loop through the array elements
- for (int i = 1; i < arr.length; i++) {
- int current = arr[i];
- if (arr[i] == arr[i - 1])
- count++;
- else {
- //compare the count with max_count
- if (count > max_count) {
- //update if count is greater
- max_count = count;
- element = arr[i - 1];
- }
- //reset count to 1
- count = 1;
- }
- }
- System.out.println();
- System.out.println(element + " " + "(" + max_count + " times)");
- }
- //output the most repeated element along with the count
- }
Advertisement
Add Comment
Please, Sign In to add comment