Nodejs Array Unique unique(mutate)

Here you can find the source of unique(mutate)

Method Source Code

/**/*from   w  w w. j  a va 2s  . c o  m*/
 * Remove duplicates from Array
 *
 * @Reference:
 * http://codereview.stackexchange.com/questions/60128/removing-duplicates-from-an-array-quickly
 * http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array/9229821#9229821
 */

Array.prototype.unique = function (mutate) {
  var unique = this.reduce(function (accum, current) {
    if (accum.indexOf(current) < 0) {
      accum.push(current);
    }
    return accum;
  }, []);
  if (mutate) {
    // Note: you cant assign `unique` variable to `this`, hence we make it's length to zero and push values to it.
    this.length = 0;
    for (var i = 0; i < unique.length; ++i) {
      this.push(unique[i]);
    }
    return this;
  }
  return unique;
};

// "Smart" but naive way
// Although concise, this algorithm is not particularly efficient for large arrays (quadratic time)
var uniqueArray = arr.filter(function (item, pos) {
  return arr.indexOf(item) === pos;
});

// Hashtables to the rescue
function uniq(a) {
  var seen = {};
  return a.filter(function (item) {
    return seen.hasOwnProperty(item) ? false : (seen[item] = true);
  });
}

// ES6 version
function unique(array) {
  return (new Set(array));
}

Related

  1. unique(b)
    Array.prototype.unique = function(b) {
      var a = [], i, l = this.length;
      for (i = 0; i < l; i++) {
        if (a.indexOf(this[i], 0, b) < 0) {
          a.push(this[i]);
      return a;
    };
    ...
    
  2. unique(callback)
    Array.prototype.unique = function(callback) {
      var o = {}, i, l = this.length, r = [];
      for (i=0; i<l; i+=1) {
        var val = callback(this[i]);
        o[val] = this[i];
      for (i in o) {
        r.push(o[i]);
      return r;
    };
    
  3. unique(data)
    Array.prototype.unique = function(data) {
      data.sort();
      var re = [ data[0] ];
      var length = data.length;
      for (var i = 1; i < length; i++) {
        if (data[i] !== re[re.length - 1]) {
          re.push(data[i]);
      return re;
    };
    
  4. unique(fun, map)
    (
      Array.prototype.unique = function (fun, map) {
        fun = fun || (function(c) { return c; });
        var arrayUnique = [this[0]], 
        arrayUniqueAtt = [fun(this[0])];
        this.forEach(function(el){
          if (arrayUniqueAtt.indexOf(fun(el)) === -1) {
            arrayUniqueAtt.push(fun(el));
            arrayUnique.push(el);
    ...
    
  5. unique(k)
    Array.prototype.unique = function(k) {
        var self = this;
        var result = [];
        var sublen = 0;
        for (var i = 0, l = self.length; i < l; i++) {
            var v = self[i];
            if (!k) {
                if (result.indexOf(v) === -1) {
                    result.push(v);
    ...
    
  6. unique(str)
    Array.prototype.unique = function(str){
      const seen = new Map()
      return this.filter((a) => {
        return !seen.has(a['name']) && seen.set(a['name'], 1)
      })
    
  7. unique1()
    Array.prototype.unique1 = function(){
      var res = [this[0]];
      for(var i = 1; i < this.length; i++){
        var repeat = false;
        for(var j = 0; j < res.length; j++){
          if(this[i] == res[j]){
            repeat = true;
            break;
        if(!repeat){
          res.push(this[i]);
      return res;
    var arr = [1, 'a', 'a', 'b', 'd', 'e', 'e', 1, 0]
    console.log(arr.unique1());
    
  8. unique1()
    Array.prototype.unique1 = function () {
      var a = [],
        o = {},
        n = this.length;
        for (var i=0; i<n; ++i) {
          o[this[i]] = this[i];
        for (var i in o) {
          a.push(o[i]);
    ...
    
  9. unique2()
    Array.prototype.unique2 = function(){
     this.sort(); 
     var res = [this[0]];
     for(var i = 1; i < this.length; i++){
      if(this[i] !== res[res.length - 1]){
       res.push(this[i]);
     return res;
    ...