Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.concurrent.*;
- class Main {
- public static void main(String[] args) {
- System.out.println("Try programiz.pro");
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(1);
- System.out.println(duplicate(list));
- list.add(2);
- list.add(3);
- System.out.println(duplicate(list));
- }
- private static List<Integer> duplicate(List<Integer> nlist) {
- Set<Integer> ans = new HashSet<>();
- Map<Integer, Integer> map = new HashMap<>();
- for (Integer i: nlist) {
- map.computeIfAbsent(i, k-> 0);
- map.put(i, map.get(i)+1);
- }
- Iterator<Integer> itr = map.keySet().iterator();
- boolean changed = true;
- while(changed) {
- changed = false;
- Map<Integer, Integer> nextmap = new HashMap<>();
- for (Map.Entry<Integer, Integer> e: map.entrySet()) {
- int count = e.getValue();
- int k = e.getKey();
- if (count != 1) {
- k = k*count;
- changed = true;
- }
- nextmap.put(k, nextmap.getOrDefault(k, 0) + 1);
- }
- map = nextmap;
- }
- return new ArrayList<>(map.keySet());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment