XOR trick. Solution in Java


  • 0

    XOR trick.

    public class Solution {
        public int missingNumber(int[] nums) {
            int all = 0;
            for(int i = 0; i <= nums.length; i++) {
                all ^= i;
            }
            
            int arrResult = 0;
            for (int i = 0; i < nums.length; i++) {
                arrResult ^= nums[i];
            }
            
            return all ^ arrResult;
        }
    }
    

Log in to reply
 

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