Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. function isPrime (n) {
  2. if (n < 2) return false
  3.  
  4. const limit = Math.floor(Math.sqrt(n))
  5.  
  6. for (let i = 2; i <= limit; i += 1) {
  7. if (n % i === 0) {
  8. return false
  9. }
  10. }
  11. return true
  12. }
  13.  
  14. function reducePrimeOccurrences (A, B) {
  15. const counts = {}
  16.  
  17. for (let i = 0; i < B.length; i += 1) {
  18. const num = B[i]
  19. counts[num] = counts[num] ? counts[num] + 1 : 1
  20. }
  21.  
  22. return A.reduce((acc, el) => isPrime(counts[el] || 0) ? acc : [...acc, el], [])
  23. }
  24.  
  25. const A = [2, 3, 9, 2, 5, 1, 3, 7, 10]
  26. const B = [2, 1, 3, 4, 3, 10, 6, 6, 1, 7, 10, 10, 10]
  27. const C = reducePrimeOccurrences(A, B)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement