Advertisement
Guest User

MaxSequenceOfEqualElements Refactor

a guest
Nov 22nd, 2019
1,967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class MaxSequenceOfEqualElements {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         int[] numbers = Arrays
  9.                 .stream(scan.nextLine().split(" "))
  10.                 .mapToInt(Integer::parseInt)
  11.                 .toArray();
  12.                
  13.         int maxCount = 0;
  14.         int digit = 0;
  15.         int counter = 1;
  16.        
  17.         for (int i = 1; i < numbers.length; i++) {
  18.             if (numbers[i] == numbers[i - 1]) {
  19.                 counter++;
  20.                
  21.                 if (counter > maxCount) {
  22.                     maxCount = counter;
  23.                     digit = numbers[i];
  24.                 }
  25.             } else
  26.                  counter = 1;
  27.         }
  28.        
  29.         for (int i = 0; i < maxCount; i++) {
  30.             System.out.print(digit + " ");
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement