Javascript Array removeDups()

Description

Javascript Array removeDups()

Array.prototype.removeDups = function () {
  var len = this.length, arr = [], obj = {}, i;
  for (i = 0; i < len; ++i) {
    obj[this[i]] = 0;/*from www .java  2s  .  co  m*/
  }
  for (i in obj) {
    arr.push(i);
  }
  return arr;
};

Javascript Array removeDups()

// dups/*  w  ww.  j a  v a2 s .  c  o m*/
Array.prototype.removeDups = function() {
  var uniques = [];

  for (var i = 0; i < this.length; i++) {
    if (uniques.length === 0 || uniques.indexOf(this[i]) === -1) {
      uniques.push(this[i]);
    }
  }

  return uniques;
};



PreviousNext

Related