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).

Knowing n ^ n = 0 and n ^ 0 = n this would be very simple.

public int missingNumber(int[] nums) { int result = 0; for (int i = 1; i <= nums.length; i++) { result ^= nums[i - 1]; result ^= i; } return result; }

Nice solution!

I think we can take this problem as a MATH problem, the sum should be n*(n+1)/2, then minus each of the element in the array.

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

You might get an overflow when n is large enough. :)

very good solution

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