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> set1 = new HashSet<Integer>(){{add(1);add(3);add(5);add(7);}};
  28. Set<Integer> set2 = new HashSet<Integer>(){{add(2);add(3);add(6);add(7);}};
  29. Set<Integer> set3 = new HashSet<Integer>(){{add(4);add(5);add(6);add(7);}};
  30.  
  31. Set<Integer> setu = unique(set1, set2, set3);
  32.  
  33. System.out.println("Input:");
  34. System.out.println(set1);
  35. System.out.println(set2);
  36. System.out.println(set3);
  37. System.out.println("Output:");
  38. System.out.println(setu);
  39. }
  40. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
Input:
[1, 3, 5, 7]
[2, 3, 6, 7]
[4, 5, 6, 7]
Output:
[1, 2, 4]