Guest User

Untitled

a guest
Apr 16th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.sql.Array;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int element = Integer.MIN_VALUE, max_count = 1, count =1;
  10.         //intial array
  11.         int a = Integer.parseInt(scanner.nextLine());
  12.         int[] arr = new int[a];
  13.         for (int i = 0; i < arr.length; i++) {
  14.             a = Integer.parseInt(scanner.nextLine());
  15.             arr[i] += a;
  16.         }
  17.  
  18.         //sort array in the ascending Order
  19.         Arrays.sort(arr);
  20.  
  21.         //loop through the array elements
  22.         for (int i = 1; i < arr.length; i++) {
  23.             int current = arr[i];
  24.  
  25.             if (arr[i] == arr[i - 1])
  26.                 count++;
  27.             else {
  28.                 //compare the count with max_count
  29.                 if (count > max_count) {
  30.                     //update if count is greater
  31.                     max_count = count;
  32.                     element = arr[i - 1];
  33.                 }
  34.                 //reset count to 1
  35.                 count = 1;
  36.             }
  37.         }
  38.         System.out.println();
  39.         System.out.println(element + " " + "(" + max_count + " times)");
  40.     }
  41.  
  42.     //output the most repeated element along with the count
  43. }
Advertisement
Add Comment
Please, Sign In to add comment