# 136. Single Number ## 題目概要 給定一個非空整數陣列 nums,每個元素都出現兩次,除了其中一個數。題目要求找到唯一一個不重複的整數的。 ``` Example 1: Input: nums = [2,2,1] Output: 1 Example 2: Input: nums = [4,1,2,1,2] Output: 4 Example 3: Input: nums = [1] Output: 1 ``` ## 解題技巧 - 因為每個數字出現的次數除了一次就是兩次,所以可以用 XOR 來解,若出現兩次相同的數字 XOR 就會為 0,最後剩下的結果就會是唯一一個不重複的整數。 ## 程式碼 ```javascript= var singleNumber = function(nums) { return nums.reduce((prev, cur) => prev ^ cur); }; ``` ![](https://i.imgur.com/gDo6NqU.png)