题目
找出数组 arr 中重复出现过的元素(不用考虑返回顺序)
示例1
输入:
[1, 2, 4, 4, 3, 3, 1, 5, 3] 输出: [1, 3, 4]
编辑
核心代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>查找重复元素</title> </head> <body> <script>function duplicates(arr) { var temp = []; arr.forEach(function (elem) { if (arr.indexOf(elem) != arr.lastIndexOf(elem) && temp.indexOf(elem) == -1) { temp.push(elem); } }); return</script> </body> </html>