FED39. 移除数组中的元素
描述
移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果数组返回示例1
输入:
[1, 2, 2, 3, 4, 2, 2], 2
输出:
[1, 3, 4]
HTML/CSS/JavaScript 解法, 执行用时: 854ms, 内存消耗: 77824KB, 提交时间: 2020-11-18
{"css":"","js":"function removeWithoutCopy(arr, item) {\n var index = arr.indexOf(item);\n var count = 0;\n for(var i=0; i < arr.length; i++) {\n if(arr[i] == item)\n count++;\n }\n arr.splice(arr.indexOf(item), count);\n return arr;\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77772KB, 提交时间: 2020-12-29
{"css":"","js":"function removeWithoutCopy(arr, item) {\nfor(let i=0;i<arr.length;i++){\n if(arr[i]==item){\n arr.splice(i,1)\n i--\n }\n}\n return arr\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77772KB, 提交时间: 2020-12-25
{"css":"","js":"function removeWithoutCopy(arr, item) {\n arr.forEach((ele,index)=>{\n if(ele=item){\n arr.splice(index,1)\n }\n index--\n }) \n return arr\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77772KB, 提交时间: 2020-12-21
{"css":"","js":"function removeWithoutCopy(arr, item) {\n for(let i=0;i<arr.length;i++){\n if(arr[i]==item){\n arr.splice(i,1)\n i--\n }\n }\n return arr\n}","html":"","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77772KB, 提交时间: 2020-11-23
{"css":"","js":"function removeWithoutCopy(arr, item) {\n for(let i =0; i< arr.length; i++){\n if(arr[i]==item){\n arr.splice(i,1)\n i--\n }\n }\n return arr\n}","html":"","libs":[]}