fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static <K,V> Set<K> keysOfDupValues(Map<K,V> m) {
  7. Set<K> res = new HashSet<K>();
  8. Map<V,K> seen = new HashMap<V,K>();
  9. for (Map.Entry<K,V> e : m.entrySet()) {
  10. V v = e.getValue();
  11. K k = e.getKey();
  12. if (seen.containsKey(v)) {
  13. res.add(k);
  14. res.add(seen.get(v));
  15. } else {
  16. seen.put(v, k);
  17. }
  18. }
  19. return res;
  20. }
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. Map<Integer,Integer> m = new HashMap<Integer,Integer>();
  24. m.put(100, 1);
  25. m.put(200, 2);
  26. m.put(300, 3);
  27. m.put(400, 2);
  28. Set<Integer> x = keysOfDupValues(m);
  29. for (Integer i : x) {
  30. System.out.println(i);
  31. }
  32. }
  33. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
200
400