列表

详情


FED57. 移除数组中的元素

描述

移除数组 arr 中的所有值与 item 相等的元素。不要直接修改数组 arr,结果返回新的数组

示例1

输入:

[1, 2, 3, 4, 2], 2

输出:

[1, 3, 4]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77900KB, 提交时间: 2020-12-04

{"css":"","js":"function remove(arr, item) {\nvar arr1 = [];\n    for(var i = 0; i < arr.length; i ++){\n        if(arr[i] !== item){\n            arr1.push(arr[i])\n        }   \n    }\n    return arr1\n}\nremove([1, 2, 3, 4, 2], 2)","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 859ms, 内存消耗: 77856KB, 提交时间: 2020-11-22

{"css":"","js":"function remove(arr, item) {\n    return arr.filter(function(val,index,arr){\n        return val!==item\n    })\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77908KB, 提交时间: 2020-12-04

{"css":"","js":"function remove(arr, item) {\n    return (arr.filter(ite=>item!=ite));\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 862ms, 内存消耗: 77772KB, 提交时间: 2020-11-22

{"css":"","js":"function remove(arr, item) {\n    return arr.filter(value=>{\n        if(value!==item){\n            return value\n        }\n    })\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 862ms, 内存消耗: 77812KB, 提交时间: 2020-12-24

{"css":"","js":"function remove(arr, item) {\n    var newArr=[];\n       for(var i=0;i<arr.length;i++){\n           if(arr[i]!=item){\n               newArr.push(arr[i]);\n           }\n       }\n    return newArr;\n}","html":"","libs":[]}

上一题