Skip to content

Instantly share code, notes, and snippets.

@sienki-jenki
Created January 24, 2021 18:13
Show Gist options
  • Save sienki-jenki/d1645d743485f30c295d18ebc1fb5673 to your computer and use it in GitHub Desktop.
Save sienki-jenki/d1645d743485f30c295d18ebc1fb5673 to your computer and use it in GitHub Desktop.
SWM
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