1// findSmallestMissingNumber finds the smallest missing positive integer in the array
2func findSmallestMissingNumber(nums []int) int {
3 n := len(nums)
4
5 // Traverse the array, placing each number in its correct position
6 for i := 0; i < n; i++ {
7 for nums[i] > 0 && nums[i] <= n && nums[nums[i]-1] != nums[i] {
8 nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
9 }
10 }
11
12 // Traverse the array again to find the first number not in its correct position
13 for i := 0; i < n; i++ {
14 if nums[i] != i + 1 {
15 return i + 1
16 }
17 }
18
19 // If the array contains numbers from 1 to n, the smallest missing number is n+1
20 return n + 1
21}
💬Discussion