136. Single number solution

  • 1

    Java Solution: O(n)

    The solution is based on a simple bitwise operation XOR as "xor of two same number is 0". In the solution we are iterating over array and xor all the elements, at the end all the duplicate numbers will result in zero and the result with be the number that does not have a duplicate.

    public int singleNumber(int[] nums) {
            int sum = nums[0];
            for(int i=1; i< nums.length; i++){
                sum = sum ^ nums[i];
            return sum;

Log in to reply

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