Advertisement
UKTC162

MostCommonNumber

Dec 12th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MostCommonNumber {
  4.     public static void main(String[] args) {
  5.  
  6.         Scanner sc = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(sc.nextLine());
  9.         int[] a = new int[n];
  10.         for (int i = 0; i < a.length; i++) {
  11.             a[i] = Integer.parseInt(sc.nextLine());
  12.         }
  13.  
  14.         int count = 1, tempCount;
  15.         int popular = a[0];
  16.         int temp = 0;
  17.         for (int i = 0; i < (a.length); i++) {
  18.             temp = a[i];
  19.             tempCount = 0;
  20.             for (int j = 0; j < a.length; j++) {
  21.                 if (temp == a[j])
  22.                     tempCount++;
  23.             }
  24.             if (tempCount > count) {
  25.                 popular = temp;
  26.                 count = tempCount;
  27.             }
  28.         }
  29.  
  30.         System.out.println(popular);
  31.  
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement