website/content/ChapterFour/0100~0199/0136.Single-Number.md
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。要求算法时间复杂度是线性的,并且不使用额外的辅助空间。
package leetcode
func singleNumber(nums []int) int {
result := 0
for i := 0; i < len(nums); i++ {
result ^= nums[i]
}
return result
}