Java O(1) space, O(n) time


  • 0
    V
    public class Solution {
        public int[] singleNumber(int[] nums) {
            int xor = 0, i = 0;
            while(i < nums.length) xor ^= nums[i++];
            
            int x = (xor & (xor - 1)) ^ xor;
            i = 0;
            int[] res = new int[2];
            while (i < nums.length) {
                if ((x & nums[i]) != 0) {
                    res[0] ^= nums[i];
                } else {
                    res[1] ^= nums[i];
                }
                
                i++;
            }
            
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.