1. 找出数组 arr 中重复出现过的元素(不用考虑返回顺序)
输入:[1, 2, 4, 4, 3, 3, 1, 5, 3]
输出:[1, 3, 4]
// ● indexOf:返回某个字符在字符串中首次出现的位置, 如果没有则返回-1;// ● lastIndexOf:返回字符在字符串中最后出现的位置;// ● 如果说字符在首次出现和最后出现的位置是不同的, 说明,数组中至少存在2个相同的元素;// ● res.indexOf(item) == -1 防止有重复的元素const arr= [1, 2, 4, 4, 3, 3, 1, 5, 3]const res = []arr1.forEach(item => {if(arr.indexOf(item) != arr.lastIndexOf(item) && res.indexOf(item) == -1){res.push(item)}})
// slice(start, end): 提取字符串的某个部分, 并且返回新的字符串(被提取的部分)//思想: 比如拿到第一个元素, 那么在剩余的元素中找到和第一个元素一样的元素, 说明数组中至少有2个元素是相同的const arr= [1, 2, 4, 4, 3, 3, 1, 5, 3]const res = []arr.forEach((item, index) => {const arr2 = arr.slice(index+1, arr.length+1);if(arr2.includes(item) && !res.includes(item)){res.push(item)}})console.log(res)
