题解 | #52.数组中只出现一次的两个数字#
数组中只出现一次的两个数字
http://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8
题目:一个整型数组里除了两个数字只出现一次,其他的数字都出现了两次。
对于数组的每一个元素,如果set中有,则删除,没有则加入
最后遍历set将value加入结果数组中返回
function FindNumsAppearOnce( array ) {
let set = new Set();
array.forEach(item=>{
if( set.has(item) )
set.delete(item);
else
set.add(item)
})
let ans = [];
for(let item of set){
ans.push(item);
}
return ans.sort((a,b)=>a-b);
}
module.exports = {
FindNumsAppearOnce : FindNumsAppearOnce
};
查看25道真题和解析