Skip to content

Instantly share code, notes, and snippets.

@tchayen
Created April 27, 2017 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tchayen/cce80d19745fb2875915330c066a38f2 to your computer and use it in GitHub Desktop.
Save tchayen/cce80d19745fb2875915330c066a38f2 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Solution {
private static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0 || n % (n - i) == 0)
return false;
return true;
}
private static List<Integer> filterPrimeTimesOccurences(List<Integer> a, List<Integer> b) {
HashMap<Integer, Integer> countingInB = new HashMap<>();
// Store occurences number for each number in B.
for (int value : b) {
countingInB.putIfAbsent(value, 0);
int count = countingInB.getOrDefault(value, 0);
countingInB.put(value, count + 1);
}
// Filter a.
List<Integer> filteredA = new ArrayList<>();
for (int value : a) {
if (!isPrime(countingInB.getOrDefault(value, 0)))
filteredA.add(value);
}
return filteredA;
}
public static void main(String[] args) {
Integer[] A = new Integer[] {2,3,9,2,5,1,3,7,10};
List<Integer> a = new ArrayList<>(Arrays.asList(A));
Integer[] B = new Integer[] {2,1,3,4,3,10,6,6,1,7,10,10,10};
List<Integer> b = new ArrayList<>(Arrays.asList(B));
filterPrimeTimesOccurences(a, b).forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment