Skip to content

Instantly share code, notes, and snippets.

@cryptoquick
Created May 6, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cryptoquick/9bdf8b5435441ea67a89f2463e3dc317 to your computer and use it in GitHub Desktop.
Save cryptoquick/9bdf8b5435441ea67a89f2463e3dc317 to your computer and use it in GitHub Desktop.
Omit Multiples (JS)
const input = [1, 1, 1, 2, 3, 3, 5, 9]
const omitMultiples = list => {
// Count the numbers in the list
const counts = list
.reduce((acc, el) => {
const item = acc[el] || 0
acc[el] = item + 1
return acc
}, {})
// Add only numbers that occur once
const results = []
for (let c in counts) {
const count = counts[c]
if (count === 1) {
results.push(c)
}
}
return results
}
console.log(omitMultiples(input))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment