Easy Java Bit Solution


  • 0
    C
    public class Solution {
        public int[] singleNumber(int[] nums) {
            if(nums.length<2) return nums;
            int xor = 0;
            for(int i=0; i<nums.length; i++){
                xor^=nums[i];
            }
            int xorZeroGroup = 0;
            int xorOneGroup = 0;
            int rightMostSetNum = xor & ~(xor-1);
            for(int i=0; i<nums.length; i++){
                if((nums[i] & rightMostSetNum) ==0){
                    xorZeroGroup^=nums[i];
                }
                else{
                    xorOneGroup^=nums[i];
                }
            }
            return new int[]{xorZeroGroup,xorOneGroup};
        }
    }
    

Log in to reply
 

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