Skip to content

Instantly share code, notes, and snippets.

@sofish
Last active July 11, 2017 01:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sofish/d02590737898ecaf0760 to your computer and use it in GitHub Desktop.
Save sofish/d02590737898ecaf0760 to your computer and use it in GitHub Desktop.
Array Unique
function unique_keys(array) {
var values = {};
for(var i = 0; i < array.length; i++) {
values[array[i]] = null;
}
return Object.keys(values);
}
function unique_reduce(array) {
return array.reduce(function(ret, cur) {
if(ret.indexOf(cur) === -1) ret.push(cur);
return ret;
}, []);
}
// test
var array = [1,2,3,4,5,6,23,1,4];
console.log(unique_keys(array), unique_reduce(array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment