6. leetcode 136. Single Number

时间:2021-11-03 16:19:32

Given an 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?

思路:将数组元素逐个异或。

举个栗子:2  3  4  2  3

所有数字依次异或运算:2 xor 3 xor 4 xor
2 xor 3 = (2 xor 2) xor (3 xor 3) xor 4= 0 xor 0 xor 4 = 4

最终结果4就是我们要找的那个只出现一次的数字。