Created
January 24, 2021 18:13
-
-
Save sienki-jenki/d1645d743485f30c295d18ebc1fb5673 to your computer and use it in GitHub Desktop.
SWM
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
const isPrime = (num) => { | |
for (let i = 2, s = Math.sqrt(num); i <= s; i++) | |
if (num % i === 0) return false; | |
return num > 1; | |
}; | |
const func = (A, B) => { | |
const obj = {}; | |
B.forEach((e) => { | |
if (!obj.hasOwnProperty(e)) { | |
obj[e] = 1; | |
} else { | |
obj[e]++; | |
} | |
}); | |
// check if prime once, instead A.length times | |
for (const key in obj) { | |
if (isPrime(obj[key])) { | |
obj[key] = -1; | |
} | |
} | |
return A.filter((e) => { | |
return obj[e] !== -1; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment