Very easy and effective solution with XOR


  • 0
    W
    public int singleNumber(int[] nums) {
        Arrays.sort(nums);
        for(int i = 0; i < nums.length - 2; i++) {
            int p1 = nums[i++]; //0
            int p2 = nums[i++]; //1
            int p3 = nums[i];   //2
            int ret1 = p1 ^ p2;
            int ret2 = p2 ^ p3;
            if(ret1 != 0 || ret2 != 0) {
                return p1;
            }
        }
        
        return nums[nums.length - 1];
    }

  • 1
    D

    It is not linear runtime complexity since you sort the array which is o(nlgn)


Log in to reply
 

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