暴力解法
/**
* @param {number[]} nums
* @return {number[]}
*/
var findDuplicates = function(nums) {
let arr =[]
let result =[]
nums.forEach(data=>{
arr.includes(data) ? result.push(data) : arr.push(data)
})
return result
};
哈希表
/**
* @param {number[]} nums
* @return {number[]}
*/
var findDuplicates = function(nums) {
const map = new Map()
const arr = []
for(let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) {
arr.push(nums[i])
map.delete(nums[i])
} else {
map.set(nums[i])
}
}
return arr
};