# Problem 136: Single Number

> [https://leetcode.com/problems/single-number/](https://leetcode.com/problems/single-number/)

## 思路

*   这道题用了异或关系的特性

    ![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9xo7VPWuHrq32ji9%2FXOR.jpg?generation=1569729544700432\&alt=media)
* 异或关系性质

![](https://1241747088-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lpv9LvBSlFaukf_ALqh%2F-Lpv9NPw3Ji1X5Vk8CES%2F-Lpv9xo9FhgqYlmIksvi%2FXOR1.jpg?generation=1569729545017877\&alt=media)

* 这道题我们就用最后两个的性质，最后剩下的就是那个落单的数字

```java
public class Solution {
    public int singleNumber(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }

        int result = nums[0];
        for (int i = 1; i < nums.length; i++) {
            result ^= nums[i];
        }
        return result;
    }
}
```
