Using XOR (Exclusive OR).
XOR is commutative, that is, A^B^A = A^A^B = 0^B = B (A and B are integers).
public class Solution {
public int singleNumber(int[] nums) {
int ret = 0;
for (int num : nums)
ret ^= num;
return ret;
}
}