const targetArr = [1, 2, 3, 5, 2, 3, 2, 9]; // 指定数组
function findOnly(arr) { // 找出指定数组中所有不重复的元素
const result = [] // 存放不重复的元素
let index
arr.forEach(item => { // 遍历该数组
index = 0
arr.map(ele => { // 再判断是否为重复元素
if(ele === item) index++
})
if (index === 1) { // 找到不重复元素
result.push(item)
}
})
return result
}
console.log(findOnly(targetArr));
console.log(findOnly(targetArr).length) // 不重复元素的个数
第7天 写一个方法找出指定一维数组所有不重复的元素和个数
最新推荐文章于 2022-01-10 14:30:07 发布