class Solution {
public int singleNumber(int[] nums) {
//there are two same elemets and find the unique one
//Using xor: same elemet will be 0, different will be one
//and further more aa^a^b = b
int res = 0;
for(int num : nums){
res ^= num;
}
return res;
}
}
- using hahet, push unmet element and pop same elemnt, then only unique element left
Runtime: 0 ms, faster than 100.00% of Java online submissions for Single Number.
Memory Usage: 39.2 MB, less than 81.48% of Java online submissions for Single Number.