原题链接: [https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/](https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/) 解题思路: 1. 遍历`nums`,使用`Set`保存第一次遇到的数字。 2. 如果第二次遇到相同数字,将其从`Set`中删除。 3. 最终`Set`中只会留下`2`个数字,即为只出现了一次的数字。 ```javascript /** * @param {number[]} nums * @return {number} */ var singleNumber = function (nums) { let set = new Set() // 使用哈希表保存遍历时遇到的数字 for (const num of nums) { // 如果遇到出现过的数字,将其从Set中删除 if (set.has(num)) { set.delete(num) } else { // 第一次遇到的数字,将其加入Set set.add(num) } } // 最终Set中只剩下2个只出现过一次的元素 return [...set] } ```