Venciity

[SoftUni Java] Collections Basic - 03.LargestSequenceEqual

May 28th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.ArrayList;
  3.  
  4. public class _03_LargestSequenceEqualStrings {
  5.    
  6.     public static void main(String[] args) {
  7.        
  8.         try(Scanner reader = new Scanner(System.in)){
  9.            
  10.             String input = reader.nextLine();
  11.            
  12.             String[] elements = input.split(" ");
  13.             ArrayList<String> lonSeq = new ArrayList<>();
  14.            
  15.             String lastElement = elements[0];
  16.             int countEqual = 0;
  17.            
  18.             for (String element : elements) {
  19.                
  20.                 countEqual++;
  21.                 if (element.equals(lastElement)) {
  22.                    
  23.                     if (countEqual > lonSeq.size()) {
  24.                        
  25.                         lonSeq = new ArrayList<String>();
  26.                        
  27.                         for (int i = 0; i < countEqual; i++) {
  28.                            
  29.                             lonSeq.add(element);
  30.                         }
  31.                     }
  32.                 }else {
  33.                     countEqual = 1;
  34.                 }
  35.                
  36.                 lastElement = element;
  37.             }
  38.             for (String string : lonSeq) {
  39.                
  40.                 System.out.print(string + " ");
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment