A simple solution, using 2 properties of XOR: A ⊕ A = 0 and B ⊕ 0 = B In other words, A ⊕ A ⊕ B = B
class Solution { public int singleNumber(int[] nums) { for(int i = 0; i< nums.length-1; i++){ nums[i+1] ^= nums[i]; } return nums[nums.length-1]; } }
Last updated 5 years ago