Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- System.out.println(joinTableWithoutPrimes(new int[]{2,3,9,2,5,1,3,7,10}, new int[]{2, 1, 3, 4, 3, 10, 6, 6, 1, 7, 10, 10, 10}));
- }
- static List<Integer> joinTableWithoutPrimes(int[] A, int[] B){
- //Declaration and variable assignment
- int aLength = A.length;
- int bLength = B.length;
- List<Integer> C = new LinkedList<>();
- Map<Integer, Integer> bExistTimesMap = new HashMap<>();
- //Creating map holding Key(Integer from B array) and Value(quantity of Key existing in B array)
- for (int i=0; i<bLength; i++) {
- if (i==0 || !bExistTimesMap.containsKey(B[i])){
- bExistTimesMap.put(B[i], 1);
- } else {
- bExistTimesMap.replace(B[i], bExistTimesMap.get(B[i])+1);
- }
- }
- //Checking if each element of array A exist in B array(map created before), and if so, counting if prime number of times.
- for (int i = 0; i<aLength; i++) {
- if (bExistTimesMap.containsKey(A[i]) && isPrime(bExistTimesMap.get(A[i]))){
- continue;
- } else {
- C.add(A[i]);
- }
- }
- return C;
- }
- //Method for prime number verification
- static boolean isPrime(int x){
- if (x<2 || (x%2==0 && x!=2)) {
- return false;
- }
- for(int i=3;i*i<=x;i+=2) {
- if(x%i==0)
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement