Qestion:
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?
Solution:
Using 0 to "XOR" calculate all elements.0 "XOR" any number, will run as adding function. But if one number exist twice, the result will be 0. For example "0101", 0000^0101 = 0101 0101 ^ 0101 = 0.
public class Solution { public int singleNumber(int[] A) { if(A == null || A.length == 0){ return 0; } int num = 0; for(int a : A){ num = num ^ a; } return num; }}