Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

public class Solution { public int missingNumber(int[] nums) { int n = nums.length; int sum = (n)*(n+1)/2; for(int i=0; i<n; i++){ sum -= nums[i]; } return sum; } }

I also use this approach, but it n = Integer.MaxInt, then it may overflow.

if there is such constraint, then XOR would work too!

Agree! But could you post a XOR solution? I am not familiar with that....

xor solution

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

I have posted xor solution below. Idea is based on A^A = 0

Cool! Thank you!

But how do you figure out this XOR solution?

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