Given an array of integers, every element appearstwiceexcept for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Subscribeto see which companies asked this question.
Hide Tags
Hide Similar Problems
public class Solution {
public int singleNumber(int[] nums) {
/**
* 异或运算符^:两个操作数的位中,相同则结果为0,不同则结果为1
*
* 所以
* n^n = 0
* n^0 = n
*/
int result = 0;
for(int num : nums){
result ^= num;
}
return result;
}
}