Advertisement
coasterka

#4LargestSequenceOfEqualStrings

Jun 3rd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map.Entry;
  3. import java.util.Scanner;
  4.  
  5. public class LargestSequenceOfEqualStrings {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.         String[] input = (scan.nextLine()).split(" ");
  10.         HashMap<String, Integer> wordCounter = new HashMap<String, Integer>();
  11.        
  12.         for (String word : input) {
  13.                 Integer counter = wordCounter.get(word);
  14.                 if(counter == null){
  15.                         counter = 0;
  16.                 }
  17.                 wordCounter.put(word, counter + 1);
  18.         }
  19.        
  20.         Entry<String, Integer> maxEntry = null;
  21.  
  22.         for(Entry<String, Integer> entry : wordCounter.entrySet()) {
  23.             if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
  24.                 maxEntry = entry;
  25.             }
  26.         }
  27.        
  28.         for (int i = 0; i < maxEntry.getValue(); i++) {
  29.                 System.out.print(maxEntry.getKey() + " ");
  30.         }
  31.  
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement