---
# System prepended metadata

title: \#136 Single Number 找出未重複的數字
tags: [兩兩相消, leetcode, leetcode-easy, XOR]

---

# \#136 Single Number 找出未重複的數字
## *給定一數字陣列, 找出陣列中的"未重複出現的元素"*
## Log
 - build 20210403 by syhuang

## 神解:XOR
 - 利用運算元XOR的特性, 單層迴圈就可以排除掉重複出現的數字, 使時間複雜度變為O(n)
```javascript=
var singleNumber = function(nums) {
    var result;
    for(var i=0; i<nums.length; i++){
        result ^= nums[i];
    }
    return result;
};
```
## 初見
 - solve by map+loop
```javascript=
var singleNumber = function(nums) {
    var map = new Map();
    var result;
    for(var i=0; i<nums.length; i++){
        map.set(nums[i],map.has(nums[i])?2:1);
    }
    map.forEach(function(val,key){
        if(val==1) result = key;
    });
    return result;
};
```
## 備註
## 參考
 - [XOR解法參考來源](https://leetcode.com/problems/single-number/discuss/42997/My-O(n)-solution-using-XOR)
###### tags: `leetcode`, `leetcode-easy`, `XOR`, `兩兩相消`