Advertisement
Guest User

Task1

a guest
May 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         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}));
  6.     }
  7.  
  8.     static List<Integer> joinTableWithoutPrimes(int[] A, int[] B){
  9.        
  10.         //Declaration and variable assignment
  11.         int aLength = A.length;
  12.         int bLength = B.length;
  13.         List<Integer> C = new LinkedList<>();
  14.         Map<Integer, Integer> bExistTimesMap = new HashMap<>();
  15.  
  16.         //Creating map holding Key(Integer from B array) and Value(quantity of Key existing in B array)
  17.         for (int i=0; i<bLength; i++) {
  18.             if (i==0 || !bExistTimesMap.containsKey(B[i])){
  19.                 bExistTimesMap.put(B[i], 1);
  20.             } else {
  21.                 bExistTimesMap.replace(B[i], bExistTimesMap.get(B[i])+1);
  22.             }
  23.         }
  24.  
  25.         //Checking if each element of array A exist in B array(map created before), and if so, counting if prime number of times.
  26.         for (int i = 0; i<aLength; i++) {
  27.             if (bExistTimesMap.containsKey(A[i]) && isPrime(bExistTimesMap.get(A[i]))){
  28.                 continue;
  29.             } else {
  30.                 C.add(A[i]);
  31.             }
  32.         }
  33.         return C;
  34.     }
  35.  
  36.     //Method for prime number verification
  37.     static boolean isPrime(int x){
  38.         if (x<2 || (x%2==0 && x!=2)) {
  39.             return false;
  40.         }
  41.         for(int i=3;i*i<=x;i+=2) {
  42.             if(x%i==0)
  43.                 return false;
  44.         }
  45.         return true;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement