Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- public class Main {
- public static void main(String[] args) {
- Integer[] A = {2,3,9,2,5,1,3,7,10};
- Integer[] B = {2,1,3,4,3,10,6,6,1,7,10,10,10};
- System.out.println(useTheAlgorithm(A, B));
- }
- private static ArrayList<Integer> useTheAlgorithm(Integer[] listA, Integer[] listB){
- HashMap<Integer, Integer> listBWithCounters = new HashMap<>();
- for(Integer elementB : listB){
- listBWithCounters.compute(elementB, (key, val) -> (val == null) ? 1 : val + 1);
- }
- ArrayList<Integer> resultList = new ArrayList<>(Arrays.asList(listA));
- for(Integer elementA : listA){
- Integer counter = listBWithCounters.get(elementA);
- if(counter != null && isPrime(counter)){
- resultList.remove(elementA);
- }
- }
- return resultList;
- }
- private static boolean isPrime(int num){
- if(num <= 3) return num > 1;
- if(num % 2 == 0) return false;
- int i = 5;
- while(Math.pow(i, 2) <= num){
- if(num % i == 0) return false;
- i += 2;
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment