Javascript Array dups()

Description

Javascript Array dups()


Array.prototype.dups = function () {
  const uniqArr = [];//from  ww w  .j a v a  2s.  c o m
  for (var i = 0; i < this.length; i++) {
    if (!uniqArr.includes(this[i])){
      uniqArr.push(this[i]);
    }
  }

  return uniqArr;
};

console.log([1,1,1,2,2,3,4,4,3,4,5,5,5].dups()); // [1,2,3,4,5]

Javascript Array dups()

Array.prototype.dups = function() {
  var result = [];
  for(var i = 0; i < this.length; i++){
    if (result.indexOf(this[i]) === -1) {
      result.push(this[i]);//w ww.ja  va 2 s. c o m
    }
  }
  
  return result;
}

// console.log([1,1,1,2,2,2,3].dups());



PreviousNext

Related