---
# System prepended metadata

title: 448 Find All Numbers Disappeared in an Array
tags: [leetcode / easy]

---

# 448 Find All Numbers Disappeared in an Array 
###### tags: `leetcode / easy`

#### 題目說明
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.

##### **Example**

- Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]
Example 2:
- Input: nums = [1,1]
Output: [2]
 

##### **Constraints**

- n == nums.length
- 1 <= n <= 10^5
- 1 <= nums[i] <= n

:::info
**Follow up**
Could you do it without extra space and in **O(n)** runtime?
You may assume the returned list does not count as extra space.
:::


#### 解題説明
```javascript=
const findDisappearedNumbers = nums => {
  const result = [];

  for (let i = 0; i < nums.length; i++) {
    const index = Math.abs(nums[i]) - 1;
    if (nums[index] > 0) {
      nums[index] = -nums[index];
    }
  }

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] > 0) {
      result.push(i + 1);
    }
  }

  return result;
};

console.log(findDisappearedNumbers([4,3,2,7,8,2,3,1]));
```

```javascript=
function findDisappearedNumbers(nums) {
  const set = new Set();
  for (let i = 0; i < nums.length; i++) {
    set.add(i + 1);
  }
  for (const num of nums) {
    set.delete(num);
  }
  return [...set];
};

console.log(findDisappearedNumbers([4,3,2,7,8,2,3,1]));
```