TheBulgarianWolf

Max Sequence Of Equal Elements

Apr 27th, 2020
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class SoftUni {
  3.    
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         System.out.print("Enter your array here: ");
  7.         String[] stringArr = sc.nextLine().split(" ");
  8.         int[] arr = new int[stringArr.length];
  9.         for(int i = 0;i<arr.length;i++){
  10.             arr[i] = Integer.parseInt(stringArr[i]);
  11.         }
  12.         int n,j;
  13.         int maxCount = 0;
  14.         int currentCount = 0;
  15.         int startIndex = 0;
  16.         int endIndex = 0;
  17.         for(n = 0;n<arr.length-1;n++){
  18.             currentCount = 0;
  19.             for(j = n+1;j<arr.length;j++){
  20.                 if(arr[n] == arr[j]){
  21.                     currentCount++;
  22.                 }
  23.             }
  24.             if(currentCount > maxCount){
  25.                 maxCount = currentCount;
  26.                 startIndex = n;
  27.                 endIndex = n + maxCount;
  28.             }
  29.         }
  30.        
  31.         for(int k = startIndex;k<=endIndex;k++){
  32.             System.out.print(arr[k] + " ");
  33.         }            
  34.     }
  35. }
Add Comment
Please, Sign In to add comment