Created
March 6, 2013 18:52
-
-
Save inkdeep/5101971 to your computer and use it in GitHub Desktop.
javascript uniq - Native and Dojo style
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
// 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; | |
}; |
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
// 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