Sort out some simple programming problems (2)

1. Merge the arrays arr1 and arr2. Do not modify the array arr directly, and the result will return a new array

        // Using concat
        function concat(arr1,arr2){
            return arr1.concat(arr2);
        }
        // Using slice+push
        function concat(arr1,arr2){
            var newArr = arr1.slice(0);
            for(var i = 0;i< arr2.length;i++){
                 newArr.push(arr2[i]);
            }
            return newArr;
        }
        // Using slice+push.apply
        function concat(arr1,arr2){
            var newArr = arr1.slice(0);
            [].push.apply(newArr,arr2);
            return newArr;
        }
        // Normal iterative copy
        function concat(arr1,arr2){
            var newArr = [];
            for(var i=0;i<arr1.length;i++){
                newArr.push(arr1[i])
            }
            for(var j=0;j<arr2.length;j++){
                newArr.push(arr2[j])
            }
            return newArr
        }

2. Add the element item in the index of the array arr, do not modify the array arr directly, and the result will return a new array

    // Using slice+splice
      function add(arr,index,item){
          var newArr = arr.slice(0);
          newArr.splice(index,0,item);
          return newArr;
      }
    //   Using slice+concat
      function add(arr,index,item){
          return arr.slice(0,index).concat(item,arr.slice(index))
      }
    //   Using concat+splice
     function add(arr,index,item){
         var newArr  = arr.concat();
         newArr.splice(index,0,item);
         return newArr
     }
    //  Using push. Apply + apply
     function add(arr,index,item){
         var newArr = [];
         [].push.apply(newArr,arr);
         newArr.splice(index,0,item);
         return newArr
     }
    //  Normal iterative copy
    function add(arr,index,item){
        var newArr = [];
        for(var i=0;i<arr.length;i++){
            newArr.push(arr[i])
        }
        newArr.splice(index,0,item);
         return newArr
    }

3. Count the number of times the element with the value equal to item in the array arr appears

    // for cycle
    function times(arr,item){
        var count = 0;
        for(var i = 0;i<arr.length;i++){
            if(arr[i]==item){
                count++;
            }
        }
        return count;
    }
    // filter() uses the specified function to determine whether to include an item in the returned array
    function times(arr,item){
        var count = arr.filter(function(a){
            return a == item;
        })
        return count.length
    }
//    map
    function times(arr,item){
        var count = 0;
        arr.map(function(a){
            if(a == item){
                count++
            }
        })
        return count;
    }
    // forEach()
    function times(arr,item){
        var count = 0;
        arr.forEach(a => {
            a==item?count++:0
            
        });
        return count
    }

4. Find out the repeated elements in the array arr

    function find(arr){
        var newArr = arr.sort();
        var result = [];
        for(var i = 0;i<newArr.length;i++){
            if(newArr[i] == newArr[i+1]&&newArr[i]!== newArr[i-1]){
                result.push(newArr[i])
            }
        }
        return result;
    }
    function find(arr){

        // Array.prototype.filter takes three parameters, (element,index,array)
        return arr.sort().filter(function(e,i){
            return arr[i] == arr[i+1]&&arr[i]!== arr[i-1];
        })
    }

5. Find the quadratic power for each element in the array arr, do not modify the array directly, and the result will return a new array

    function square(arr){
        var newArr = arr.slice(0);
        var result = [];
        for(var i = 0 ; i<newArr.length;i++){
            result.push(arr[i]*arr[i]);  
        }
        return result;
    }
    function square1(arr){
        return arr.map(function(item,index,array){
            return item*item;
        })
    }

6. In the array arr, find all positions where the element with the same value as the item appears

    //filter  
    function findAllOccurrences(arr,item){
        var result = [];
         arr.filter(function(element,index){
            return element ==item &&result.push(index)
        })
        return result
     }
   //for cycle
    function findAllOccurrences(arr,item){
        var result = [];
        for(var i = 0;i<arr.length;i++){
            if(arr[i]==item){
                result.push(i)
            }
        }
        return result
    }

 

Posted by richinsc on Sun, 05 Jan 2020 04:52:48 -0800