Missing Number

• Approach #3, Approach #4 For the first time，I saw these kind of thinking to solve question. Brilliant！

• regarding Approach #3, I believe this will only work if:
index 0 and num 0,
index 1 has num 1,
index 5 has num 5,
index 9 has num 9
etc...

what if index 0 has 251
index 1 has 267
index 2 has 305
etc???

@jjsomiya - what do you think ?

• @seakhar The numbers do not have to be in order because XOR is a commutative function.

However, your second example does not fit the constraints of the problem (unless the array is at least 305 elements long). See: "n distinct numbers taken from 0, 1, 2, ..., n"

• This post is deleted!

• private int[] Sortarray(int[] array)
{
for(int x = 0; x < array.Length; x++)
{
for(int y = x; y < array.Length; y++)
{
if(array[y] < array[x])
{
int a = array[x];
array[x] = array[y];
array[y] = a;
}
}
}
return array;
}

``````    public int FindMissingNumber(int[] array)
{
int value = 0;
array = (Sortarray(array));
for(int x = 0; x <  array.Length; x++)
{
if(x < array.Length)
{
if(array[x + 1] == array[x] + 1)
{
continue;
}
else
{
value = array[x] + 1;
x = array.Length;
}

}
}
return value;
}``````

• simple C++ solution

``````int missingNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
for(int i = 0; i <= nums.size(); i++)
if(nums[i] != i)
return i;
}
``````

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

• class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sum(range(len(nums)+1)) - sum(nums)

• ``````    public int missingNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int len = nums.length;
int pos = len;
for (int i = 0; i < len; i++) {
while (i != nums[i] && nums[i] != len) {
int temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
if (nums[i] == len) {
pos = i;
}
}
return pos;
}
}
``````

• return (len(nums) * (len(nums)+1) >> 1) - sum(nums)

• class Solution {
public int missingNumber(int[] nums) {

``````    Arrays.sort(nums);

for (int i = 0; i < nums.length; i++) {
if (nums[i] != i) {
return i;
}
}

return nums.length;
}
``````

}

• C++

``````class Solution {
public:
int missingNumber(vector<int>& nums) {
sort(nums.begin(), nums.end());
if(nums.size() == 1){
return nums.at(0) == 0 ? 1 : 0;
}
if(*nums.begin() != 0){
return 0;
}
for(auto it = nums.begin(); it != nums.end(); ++it){
if(*(it + 1) - *it != 1){
return *it + 1;
}
}
return *(nums.end() - 1) + 1;
}
};
``````

• Approach #4 makes me high

• pproach #4 makes me high too!

• the approach #4 as same me,so, makes me high too!

• c solution

int missingNumber(int* nums, int numsSize) {

``````int sum =0 ;
double numsize = numsSize;
for(int i=0;i<numsSize; i++)
{
sum += nums[i];
}
return (1+numsSize)*numsize/2-sum ;
``````

}

• func missingNumber(_ nums: [Int]) -> Int {
return (nums.count) * (nums.count+1)/2 - nums.reduce(0, +)
}

• class Solution(object):
def missingNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = max(nums)
sum1 = sum(nums)
sum2= 0
for i in range(1,n+1):
sum2 += i

``````    if 0 not in nums:
return 0
if sum1 - sum2 == 0:
return n+1
else:
return abs(sum1-sum2)``````

• In approach #1, why need to Ensure that n is at the last index when the for loop already checks for that?

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