Skip to content

Instantly share code, notes, and snippets.

@inkdeep
Created March 6, 2013 18:52
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 inkdeep/5101971 to your computer and use it in GitHub Desktop.
Save inkdeep/5101971 to your computer and use it in GitHub Desktop.
javascript uniq - Native and Dojo style
// Native JS (fast)
uniq = function( /* Array */ arr) {
var test = {};
var result = [];
for (var i = 0, len = arr.length; i < len; i++) {
if (!test[arr[i]]) { // value not seen yet?
test[arr[i]] = true;
result.push(arr[i]);
}
}
return result;
};
// Uses dojo (not as fast)
uniq = function(/* Array */ arr){
var test = {};
return dojo.filter(arr, function(val){
return test[val] ? false : (test[val] = true);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment