Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function isPrime (n) {
- if (n < 2) return false
- const limit = Math.floor(Math.sqrt(n))
- for (let i = 2; i <= limit; i += 1) {
- if (n % i === 0) {
- return false
- }
- }
- return true
- }
- function reducePrimeOccurrences (A, B) {
- const counts = {}
- for (let i = 0; i < B.length; i += 1) {
- const num = B[i]
- counts[num] = counts[num] ? counts[num] + 1 : 1
- }
- return A.reduce((acc, el) => isPrime(counts[el] || 0) ? acc : [...acc, el], [])
- }
- const A = [2, 3, 9, 2, 5, 1, 3, 7, 10]
- const B = [2, 1, 3, 4, 3, 10, 6, 6, 1, 7, 10, 10, 10]
- const C = reducePrimeOccurrences(A, B)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement