SHARE
TWEET

Untitled

a guest Oct 10th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private void task_3(){
  2.         int numOfStrings = 5;
  3.  
  4.         try{
  5.             StringTokenizer tokenizer = new StringTokenizer(br.readLine());
  6.             Map<String, Integer> repeatNum = new HashMap<String, Integer>();
  7.             String[] stringsArray = new String[numOfStrings];
  8.             StringJoiner joiner = null;
  9.             boolean unique = true;
  10.  
  11.             for (String str:stringsArray) {
  12.                 str = tokenizer.nextToken();
  13.                 if (repeatNum.containsKey(str)) repeatNum.put(str,(repeatNum.get(str)+1));
  14.                 else repeatNum.put(str, 1);
  15.             }
  16.  
  17.             for (Map.Entry<String, Integer> entry : repeatNum.entrySet()){
  18.                 if (entry.getValue() == 5){
  19.                     unique = false;
  20.                     joiner = new StringJoiner(", ");
  21.                 }
  22.                 else if (entry.getValue() > 1 && entry.getValue() < 5){
  23.                     unique = false;
  24.                     joiner = new StringJoiner(" ");
  25.                 }
  26.                 if (!unique){
  27.                     for (int i = 0; i < entry.getValue() && entry.getValue() > 1; i++)
  28.                         joiner.add(entry.getKey());
  29.                 }
  30.             }
  31.  
  32.             if (unique) System.out.println("Все разные");
  33.             else System.out.println(joiner.toString());
  34.         }
  35.         catch(Exception ex){
  36.             System.out.println(ex.getMessage());
  37.         }
  38.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top