Facebook
From K.G., 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 51
  1. public List<Integer> function(List<Integer> A, List<Integer> B) {
  2.         List<Integer> C = new ArrayList<>();
  3.  
  4.         Map<Integer, Long> howManyTimesAppearsInList = B.stream()
  5.                 .collect(Collectors.groupingBy(x -> x, Collectors.counting()));
  6.  
  7.         for (int element : A) {
  8.             Long occurrences = howManyTimesAppearsInList.get(element);
  9.             if (occurrences == null) {
  10.                 C.add(element);
  11.             } else {
  12.                 if (occurrences == 1) {
  13.                     C.add(element);
  14.                 } else {
  15.                     for (int j = 2; j < element; j++) {
  16.                         if (element % j == 0) {
  17.                             C.add(element);
  18.                             break;
  19.                         }
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.         return C;
  25.     }