https://leetcode.com/problems/single-number/
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?
解题思路:
一个数与它自身异或等于0,将数组中的所有数字异或可以得到只出现一次的数。
public int singleNumber(int[] nums) {
int s = nums[0];
for(int i=1;i<nums.length;i++){
s = s^nums[i];
}
return s;
}