列表

详情


FED47. 查找重复元素

描述

找出数组 arr 中重复出现过的元素(不用考虑返回顺序)

示例1

输入:

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

输出:

[1, 3, 4]

原站题解

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

{"css":"","js":"function duplicates(arr) {\n    var a = [],b = [];\n     //遍历arr,如果以arr中元素为下标的的b元素已存在,则该b元素加1,否则设置为1\n     for(var i = 0; i < arr.length; i++){\n         if(!b[arr[i]]){\n             b[arr[i]] = 1;\n             continue;\n         }\n         b[arr[i]]++;\n     }\n     //遍历b数组,将其中元素值大于1的元素下标存入a数组中\n     for(var i = 0; i < b.length; i++){\n         if(b[i] > 1){\n             a.push(i);\n         }\n     }\n     return a;\n}","html":"","libs":[]}

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

{"css":"","js":"function duplicates(arr) {\n   var a=arr.sort(),b=[];\n    for(var i in a){\n        if(a[i]==a[i-1] && b.indexOf(a[i])==-1) b.push(a[i]); \n    }\n    return b;\n}","html":"","libs":[]}

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

{"css":"","js":"function duplicates(arr) {\n var arr1 = [];\n   arr.forEach(function (ele) {\n                if (arr.indexOf(ele) != arr.lastIndexOf(ele) && arr1.indexOf(ele) == -1){\n                    arr1.push(ele);\n                }\n            });\n            return arr1;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 844ms, 内存消耗: 77772KB, 提交时间: 2021-03-28

{"css":"","js":" function duplicates(arr) {\n    var temp = [];\n    arr.forEach(function(elem){\n        if(arr.indexOf(elem) != arr.lastIndexOf(elem) && temp.indexOf(elem) == -1){\n            temp.push(elem);\n        }\n    });\n        \n    \n    return temp;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 845ms, 内存消耗: 77752KB, 提交时间: 2021-02-26

{"css":"","js":"function duplicates(arr) {\n  var res = [];\n    var hashmap = new Map();\n    for(var num of arr){\n      if(hashmap.has(num) === false) hashmap.set(num ,0);\n      hashmap.set(num, hashmap.get(num)+1);\n    }\n    console.log(hashmap);\n    for(var [key, val] of hashmap){\n        if(val > 1) res.push(key);\n    }\n    return res;\n}","html":"","libs":[]}

上一题