fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6.  
  7. public static <E> Set<E> unique(Set<? extends E>... sets){
  8. Set<E> once = new HashSet<E>();
  9. Set<E> twice = new HashSet<E>();
  10.  
  11. for(Set<? extends E> set:sets){
  12. for(E el:set){
  13. if(once.contains(el)){
  14. twice.add(el);
  15. } else {
  16. once.add(el);
  17. }
  18. }
  19. }
  20.  
  21. once.removeAll(twice);
  22. return once;
  23. }
  24.  
  25. public static void main (String[] args) throws java.lang.Exception
  26. {
  27. Set<Integer> a = new HashSet<Integer>();
  28. a.add(2);
  29. a.add(4);
  30. a.add(6);
  31. a.add(8);
  32. a.add(9);
  33.  
  34. Set<Integer> b = new HashSet<Integer>();
  35. b.add(2);
  36. a.add(8);
  37. b.add(9);
  38.  
  39. Set<Integer> c = new HashSet<Integer>();
  40. c.add(2);
  41. c.add(4);
  42. c.add(8);
  43. a.add(9);
  44.  
  45. Set<Integer> s= unique(a, b, c);
  46.  
  47. for (int t : s) {
  48. System.out.print(t + ", ");
  49. }
  50. }
  51. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
6,