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?
request the linear runtime complexity, use XOR calculation, A ^ A = 0, A ^ B ^ A = B;
package leetcode;
public class SigleNumber {
public static void main(String[] args) {
int[] test = {1,3,2,2,1};
System.out.println(singleNumber(test));
}
public static int singleNumber (int[] A) {
int result = 0;
for (int i = 0; i < A.length; i++) {
result ^= A[i];
}
return result;
}
}