Created
March 5, 2020 00:44
-
-
Save Wokstym/3e15dbe800b9bbcda7de650401553b48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Optimized for not checking multiples of 3 */ | |
private static boolean isPrime(double val) { | |
if (val == 0 || val == 1) return false; | |
if (val == 2 || val == 3) return true; | |
if (val % 2 == 0 || val % 3 == 0) return false; | |
for (int i = 5; i <= Math.sqrt(val); i += 4) { | |
if (val % i == 0) return false; | |
i += 2; | |
if (val % i == 0) return false; | |
} | |
return true; | |
} | |
private static Integer[] subtractPrimeOccurrences(int []originalArray, int[] subtractorArray){ | |
/* <number, occurrences> */ | |
HashMap<Integer, Integer> subtractorArrayOccurrences = new HashMap<>(); | |
List<Integer> result = new ArrayList<>(); | |
for (int nr : subtractorArray) | |
subtractorArrayOccurrences.compute(nr, (key, val) -> val == null ? 1 : val + 1); | |
for (int nr : originalArray) { | |
int nrOfOcc = subtractorArrayOccurrences.getOrDefault(nr, 0); | |
if (!isPrime(nrOfOcc)) | |
result.add(nr); | |
} | |
return result.toArray(new Integer[0]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment