Skip to content

Instantly share code, notes, and snippets.

@hontas hontas/uniq.js

Last active Aug 29, 2015
Embed
What would you like to do?
Uniq implementation using Array.prototype.reduce
/* more functional version using concat */
function uniq(array) {
return array.reduce(function(result, currentElement) {
if (result.indexOf(currentElement) < 0) {
return results.concat([currentElement]);
}
return result;
}, []);
}
/* more traditional version using push */
function uniq2(array) {
return array.reduce(function(result, currentElement) {
if (result.indexOf(currentElement) < 0) {
results.push(currentElement);
}
return result;
}, []);
}
/* using array.filter */
function uniq3(array) {
return array.filter(function (value, index, self) {
return self.indexOf(value) === index;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.