Skip to content

Instantly share code, notes, and snippets.

@inkdeep
Created March 6, 2013 18:52
Show Gist options
  • Select an option

  • Save inkdeep/5101971 to your computer and use it in GitHub Desktop.

Select an option

Save inkdeep/5101971 to your computer and use it in GitHub Desktop.

Revisions

  1. inkdeep created this gist Mar 6, 2013.
    12 changes: 12 additions & 0 deletions js-array_uniq.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // 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;
    };
    7 changes: 7 additions & 0 deletions js-dojo_array_uniq.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    // Uses dojo (not as fast)
    uniq = function(/* Array */ arr){
    var test = {};
    return dojo.filter(arr, function(val){
    return test[val] ? false : (test[val] = true);
    });
    };