Java solution using HashSet instead of XOR


  • 9
    A
    public int singleNumber(int[] nums) {
        HashSet<Integer> check = new HashSet<Integer>();
        for(int i = 0; i < nums.length; i++)
            if(!check.remove(nums[i]))
                check.add(nums[i]);
        return check.iterator().next();
    }

  • 2
    S

    The question says " Could you implement it without using extra memory? "


Log in to reply
 

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