Nodejs Array Remove Duplicate removeDups()

Here you can find the source of removeDups()

Method Source Code

Array.prototype.removeDups = function(){

    var result = [];

    for(var i = 0; i < this.length; i++){
       if(result.indexOf(this[i]) < 0){
           result.push(this[i]);//w  w  w. j a  va 2  s  .  c o  m
       }
    }

    return result;

}

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

Array.prototype.twoSum = function(){
    result = [];
    for(var i = 0; i < this.length ; i++){
        for(var j = i + 1; j < this.length; j++){
            if(this[i] + this[j] === 0){
                result.push([i, j]);
            }
        }
    }
    return result;
}

console.log([-1, 0, 2, -2, 1].twoSum());

Array.prototype.myTranspose = function(){

    result = [];
    for(var i = 0; i < this.length; i++){
        row = [];
        for(var j = 0; j < this.length; j++){
           row.push(this[j][i]);
        }
        result.push(row);
    }
    return result;
}

console.log([[1, 2, 3], [4, 5, 6], [7, 8, 9]].myTranspose());

Related

  1. removeDuplicateValues()
    Array.prototype.removeDuplicateValues = function () {
        return this.filter(function (elem, pos, self) {
            return self.indexOf(elem) === pos;
        });
    };
    
  2. removeDuplicatedValues()
    Array.prototype.removeDuplicatedValues = function (){
        var lArray  = this || [];
        var lLength = lArray.length;
        for (var i = lLength - 1; i > -1; i--){
            if (lArray.indexOf(lArray[i]) != i){
                lArray.splice(i, 1);
        if (lLength != lArray.length){
    ...
    
  3. removeDuplicates()
    Array.prototype.removeDuplicates = function() {
        var array = this;
        return array.filter(function(elem, pos) {
            return array.indexOf(elem) == pos;
        })
    };
    
  4. removeDuplicates()
    Array.prototype.removeDuplicates = function() {
      var results = [];
      for (var i = 0; i < this.length; i++) {
        var fail = false;
        for (var j = 0; j < results.length; j++) {
          if (isSameArray(results[j], this[i])) {
            fail = true;
            break;
        if (!fail) results.push(this[i]);
      return results;
    var isSameArray = function(a1, a2) {
      if (a1.length != a2.length) return false;
      for (var i = 0; i < a1.length; i++) if (!~a2.indexOf(a1[i])) return false;
      return true;
    
  5. removeDups()
    Array.prototype.removeDups = function () {
      var len = this.length, arr = [], obj = {}, i;
      for (i = 0; i < len; ++i) {
        obj[this[i]] = 0;
      for (i in obj) {
        arr.push(i);
      return arr;
    ...
    
  6. remove_duplicates()
    Array.prototype.remove_duplicates = function() {
        var seen = [];
        for (var i = this.length; --i >= 0;) {
            var el = this[i];
            if (seen.contains(el)) this.splice(i, 1);
            seen.push(el);
        return this;
    };
    ...
    
  7. removeElement(search, func)
    Array.prototype.removeElement = function(search, func){
      var index = this.indexOfElement(search, func);
      if(index != -1) {
        var remove = this.splice(index, 1);
        return remove[0];
      return null;