---
title: 'LeetCode 136. Single Number'
disqus: hackmd
---
# LeetCode 136. Single Number
## Description
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
## Example
Input: nums = [2,2,1]
Output: 1
## Constraints
1 <= nums.length <= 3 * 10^4^
-3 * 10^4^ <= nums[i] <= 3 * 10^4^
Each element in the array appears twice except for one element which appears only once.
## Answer
此題因為每個數有重複的話只有重複兩次,所以做兩次XOR就會回復原本的,照XOR的特點從頭做到XOR到尾就可以抓出不重複的那一個。
```Cin=
// 2021_11_04
int singleNumber(int* nums, int numsSize) {
int i = 0, ans = *nums++;
for(i = 1;i < numsSize;i++,nums++){
ans ^= *nums;
}
return ans;
}
```
## Link
https://leetcode.com/problems/single-number/
###### tags: `Leetcode`