Advertisement
Spocoman

08. Most Frequent Number

Oct 27th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int[] numbers = Arrays
  8.                 .stream(scanner.nextLine().split(" "))
  9.                 .mapToInt(Integer::parseInt)
  10.                 .toArray();
  11.        
  12.         int number = 0, count, maxCount = 0;
  13.        
  14.         for (int i = 0; i < numbers.length; i++) {
  15.             count = 0;
  16.             for (int j = i; j < numbers.length; j++) {
  17.                 if (numbers[i] == numbers[j]){
  18.                     count++;
  19.                 }
  20.             }
  21.        
  22.             if (count > maxCount) {
  23.             maxCount = count;
  24.             number = numbers[i];
  25.             }
  26.         }
  27.  
  28.         System.out.println(number);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement