psO1

ss

Aug 6th, 2025
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4. class Main {
  5.     public static void main(String[] args) {
  6.         System.out.println("Try programiz.pro");
  7.        
  8.         List<Integer> list = new ArrayList<>();
  9.         list.add(1);
  10.         list.add(2);
  11.         list.add(3);
  12.         list.add(1);
  13.        
  14.         System.out.println(duplicate(list));
  15.         list.add(2);
  16.         list.add(3);
  17.         System.out.println(duplicate(list));
  18.     }
  19.    
  20.     private static List<Integer> duplicate(List<Integer> nlist) {
  21.         Set<Integer> ans = new HashSet<>();
  22.         Map<Integer, Integer> map = new HashMap<>();
  23.        
  24.         for (Integer i: nlist) {
  25.             map.computeIfAbsent(i, k-> 0);
  26.             map.put(i, map.get(i)+1);
  27.         }
  28.        
  29.         Iterator<Integer> itr = map.keySet().iterator();
  30.         boolean changed = true;
  31.         while(changed) {
  32.             changed = false;
  33.             Map<Integer, Integer> nextmap = new HashMap<>();
  34.  
  35.             for (Map.Entry<Integer, Integer> e: map.entrySet()) {
  36.                 int count = e.getValue();
  37.                 int k  = e.getKey();
  38.                
  39.                 if (count != 1) {
  40.                     k = k*count;
  41.                     changed = true;
  42.                 }
  43.                 nextmap.put(k, nextmap.getOrDefault(k, 0) + 1);
  44.             }
  45.             map = nextmap;
  46.  
  47.         }
  48.         return new ArrayList<>(map.keySet());
  49.        
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment