Created
May 6, 2019 18:04
-
-
Save cryptoquick/9bdf8b5435441ea67a89f2463e3dc317 to your computer and use it in GitHub Desktop.
Omit Multiples (JS)
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 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