fork download
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. class MatrixCommonElements {
  5.  
  6. public static void main(String[] args) {
  7. int[][] matrix = {{7, 1, 3, 5, 3, 6},
  8. {2, 3, 6, 1, 1, 6},
  9. {6, 1, 7, 2, 1, 4},
  10. {6, 6, 7, 1, 3, 3},
  11. {5, 5, 6, 1, 5, 4},
  12. {3, 5, 6, 2, 7, 1},
  13. {4, 1, 4, 3, 6, 4},
  14. {4, 6, 1, 7, 4, 3}};
  15.  
  16. Set<Integer> commonElements = toSet(matrix[0]);
  17.  
  18. for (int i = 1; i < matrix.length; i++) {
  19. commonElements.retainAll(toSet(matrix[i]));
  20. }
  21.  
  22. System.out.println(commonElements);
  23. }
  24.  
  25. private static Set<Integer> toSet(int[] intArray) {
  26. return Arrays.stream(intArray).boxed().collect(Collectors.toSet());
  27. }
  28.  
  29. }
Success #stdin #stdout 0.23s 2184192KB
stdin
Standard input is empty
stdout
[1, 6]