Hi guys. Can I improve performance of this algorithm or it is the best solution?
The task is: Array C should contain all elements from arrA (maintaining the order) except those, that are present in arrB n times, where n is a prime number.

isPrime is O(n).
countTimesInArray is O(n)
filterArray, well, O(n) for the overall loop, which then runs isPrime (which is O(n) so O(n^2)), which runs countTimesInArray (which is also O(n) so O(n^4) I think?). That isn’t quite right, as I don’t think worst-case can cause it to do a full iteration all down the stack every time.
But then you run filter again which is O(n). So O(n + n^4) maybe? It is definitely not very efficient, though it may well do fine: you do a lot of iterations, it’s brute force.

You have to consider that O(m * n), where m is countable is still O(n). This is often seems counter intuitive, at least I couldn’t get it at first on my CS classes. So this would be linear time function:

Which will give you filtered version of those that are present in arrB n times, where n is a prime number. You would need to write a function that checked if a number was prime. Alternatively, just hardcode primes up to an arbitrary number. Then create a hash map of occurences. Then filter arrA based on whether each value does not have a prime number of occurences in ArrB.

For example (spoiler, but if the primes is the only bit you’re stuck on then this shouldn’t give anything away you don’t know):

function wierdFilter (targetArray, checkArray) {
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const occurences = {};
for (const item of checkArray) {
if (item in occurences) {
occurences[item] += 1;
} else {
occurences[item] = 1;
}
};
return targetArray.filter((item) => !(primes.has(occurences[item])));
}