Nodejs Array Duplicate duplicate()

Here you can find the source of duplicate()

Method Source Code

//implement duplicate([1, 2, 3, 4, 5]) // [1,2,3,4,5,1,2,3,4,5]


function duplicate(arr) {
    var len = arr.length;
    for (var i = 0; i < len; i++) {
        console.log(arr[i]);//w  w w  .  j  ava  2s .co  m
        arr[len + i] = arr[i];
    }
    return arr;
}

// or [1,2,3].duplicate();

Array.prototype.duplicate = function () {
    var len = this.length;
    for (var i = 0; i < len; i++) {
        this[len + i] = this[i];
    }
    return this;
}

Related

  1. duplicate()
    Array.prototype.duplicate = function(){
      var newArr = this.slice();
      for(var i = 0; i < this.length; i ++){
        newArr.push(this[i]);
      return newArr;
    };
    console.log([1, 2, 3, 4].duplicate());
    
  2. duplicate()
    Array.prototype.duplicate = function(){
      return this.concat(this);
    duplicate([1,2,3,4,5])
    
  3. duplicate()
    function duplicateArray(arr) {
      return arr.concat(arr);
    Array.prototype.duplicate = function() {
      return this.concat(this);
    console.log([1,2,3,4,5].duplicate());
    console.log(duplicateArray([1,2,3,4,5]));
    
  4. duplicate()
    Array.prototype.duplicate = function(){
      var arrayVal = this;
      this.forEach(function(val){
        arrayVal.push(val);
      })
      return arrayVal;
    
  5. duplicate()
    var arr1 = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
    Array.prototype.duplicate = function () {
        return this.concat(this);
    console.log([4, 5, 6, 7, 7].duplicate());
    
  6. duplicator()
    Array.prototype.duplicator = function() {
      var len = this.length;
      for (var i = 0; i < len; i++) {
        this.push(this[i]);
      return this;
    };
    console.log([1, 2, 3, 4, 5].duplicator());
    
  7. dups()
    Array.prototype.dups = function () {
      const uniqArr = [];
      for (var i = 0; i < this.length; i++) {
        if (!uniqArr.includes(this[i])){
          uniqArr.push(this[i]);
      return uniqArr;
    };
    ...
    
  8. 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]);
      return result;