---
description: 找到消失的最小正整數
---

# # 41 First Missing Positive

[LeetCode](https://leetcode.com/problems/first-missing-positive/)\
\
Given an unsorted integer array, find the smallest missing positive integer.

**Example 1:**

```
Input: [1,2,0]
Output: 3
```

**Example 2:**

```
Input: [3,4,-1,1]
Output: 2
```

**Example 3:**

```
Input: [7,8,9,11,12]
Output: 1
```

### 怎麼解

一開始弄錯題目，原來 input 可以是不連續的，例如範例  3 所以基本上一定是從 1 開始找，若找不到就 return 1

```
/**
 * @param {number[]} nums
 * @return {number}
 */
var firstMissingPositive = function(nums) {
  for(let i = 1, set = new Set(nums); true; i++){
      if(!set.has(i)) return i
  }
};
```
