Skip to content

Instantly share code, notes, and snippets.

@apolopena
Created January 14, 2019 01:44
Show Gist options
  • Save apolopena/212b1876eeecef9be0d1b8795c0c2503 to your computer and use it in GitHub Desktop.
Save apolopena/212b1876eeecef9be0d1b8795c0c2503 to your computer and use it in GitHub Desktop.
JavaScript: return an array of unique numbers from an array of numbers using reduce() and find()
function makeUnique(array) {
return array.reduce(function(prev, num) {
if (!prev.find(function(item) { return item == num; })) {
prev.push(num);
}
return prev;
}, []);
}
var uniqueNumbers = makeUnique([1, 6 ,5, 6, 4, 3, 3, 3, 2, 1, 1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment