Skip to content

Instantly share code, notes, and snippets.

@andrew310
Created August 3, 2016 21:52
Show Gist options
  • Save andrew310/2d49cae2b6e13b0135d014e304c9148f to your computer and use it in GitHub Desktop.
Save andrew310/2d49cae2b6e13b0135d014e304c9148f to your computer and use it in GitHub Desktop.

Revisions

  1. andrew310 created this gist Aug 3, 2016.
    25 changes: 25 additions & 0 deletions dedupe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    var list = [1, 4, 8, 33, 2, 1, 5, 7, 8, 6, 3, 2];

    /*VERSION 1 */
    //retains current order of the list
    var dups = {}; //create an object, purpose is we want keys

    var uniques = list.filter(function(item){
    return dups.hasOwnProperty(item) ? false : dups[item] = true;
    });
    /* How does this work?
    / So we are using javascript objects as maps here. As you go through the list, the filter function passes the callback function each item
    / in the list to see if it should retain that item in the filtered list. If the item is not in the dups map, we add that key to the dups
    / map (dups[item] = true) and then return true so that this item will be retained in the list.
    / if the key is found in the dups map, the callback function returns false, and this current item is tossed from the list.
    */

    /*VERSION 2*/
    //does NOT retain current order. This is basically the same as the ES6 "set" function, also in Python.
    var sortedUniques = list.sort(function(a, b){return a-b;}).filter(function(item, pos, arr){
    return !pos || item != arr[pos - 1];
    })
    /* How does it work?
    / the filter function basically checks if the current item is the same as the one before it. (This works because it is sorted).
    / if it is the same, it is tossed from the list. If not, it returns true because of the or statement, so it is retained.
    /*