---
description: '# Easy'
---

# 268. Missing Number

{% hint style="info" %}
Two methods:&#x20;

1. put each element to its own place to see which number is missing
2. sum all elements and Gauss sum
{% endhint %}

{% tabs %}
{% tab title="Python-method1" %}
```python
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        nums.append(-1)
        i = 0
        while i < len(nums):
            if nums[i] == i or nums[i] == -1:
                i += 1
                continue
            tmp = nums[nums[i]]
            nums[nums[i]] = nums[i]
            nums[i] = tmp
        
        print(nums)
        for i in range(len(nums)):
            if nums[i] == -1:
                return i
```
{% endtab %}

{% tab title="Python-method2" %}
```python
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        sum1 = len(nums)*(len(nums)+1)//2
        sum2 = sum(nums)
        return sum1-sum2
        
```
{% endtab %}
{% endtabs %}

Time = $$O(n)$$ , Space = $$O(1)$$&#x20;
