Skip to content

Instantly share code, notes, and snippets.

@ovarunendra
Last active February 17, 2017 08:47
Show Gist options
  • Select an option

  • Save ovarunendra/58d8ae2e5584852db68e9950e64f2bee to your computer and use it in GitHub Desktop.

Select an option

Save ovarunendra/58d8ae2e5584852db68e9950e64f2bee to your computer and use it in GitHub Desktop.

Revisions

  1. Varunendra Pratap Singh revised this gist Feb 17, 2017. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions dedup.js
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,18 @@ function dedup(arr) {
    return arr.filter(function (el) {
    var key;
    if (Object.prototype.toString.call(el) === "[object Object]") {
    key = JSON.stringify(Object.keys(el).sort()) + JSON.stringify(Object.values(el).sort());
    } else {
    key = JSON.stringify(Object.keys(el).sort());
    Object.keys(el).forEach(function(i){
    key += JSON.stringify(el[i]);
    });
    } else {
    key = JSON.stringify(el);
    }
    var match = Boolean(hashTable[key]);
    return (match ? false : hashTable[key] = true);
    });
    }
    }

    var asd = [1, '1', 1, [1,2], [2,1], [1,2], {a:1, b:2}, {a:2, b:1}, {b:1, a:2}];

    console.log(dedup(asd));
  2. Varunendra Pratap Singh revised this gist Feb 16, 2017. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions dedup.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    function dedup(arr) {
    var hashTable = {};
    return arr.filter(function (el) {
    var key;
    if (Object.prototype.toString.call(el) === "[object Object]") {
    key = JSON.stringify(Object.keys(el).sort()) + JSON.stringify(Object.values(el).sort());
    } else {
    key = JSON.stringify(el);
    }
    var match = Boolean(hashTable[key]);
    return (match ? false : hashTable[key] = true);
    var key;
    if (Object.prototype.toString.call(el) === "[object Object]") {
    key = JSON.stringify(Object.keys(el).sort()) + JSON.stringify(Object.values(el).sort());
    } else {
    key = JSON.stringify(el);
    }
    var match = Boolean(hashTable[key]);
    return (match ? false : hashTable[key] = true);
    });
    }
  3. Varunendra Pratap Singh created this gist Feb 16, 2017.
    13 changes: 13 additions & 0 deletions dedup.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    function dedup(arr) {
    var hashTable = {};
    return arr.filter(function (el) {
    var key;
    if (Object.prototype.toString.call(el) === "[object Object]") {
    key = JSON.stringify(Object.keys(el).sort()) + JSON.stringify(Object.values(el).sort());
    } else {
    key = JSON.stringify(el);
    }
    var match = Boolean(hashTable[key]);
    return (match ? false : hashTable[key] = true);
    });
    }