Skip to content

Commit 0f14b52

Browse files
committed
added solution in golang: 41. First Missing Positive
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent ce094af commit 0f14b52

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func firstMissingPositive(nums []int) int {
4+
i := 0
5+
6+
for i < len(nums) {
7+
if nums[i] > 0 && nums[i] <= len(nums) && nums[nums[i]-1] != nums[i] {
8+
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
9+
} else {
10+
i++
11+
}
12+
}
13+
14+
for i, num := range nums {
15+
if num != i+1 {
16+
return i + 1
17+
}
18+
}
19+
20+
return len(nums) + 1
21+
}

0 commit comments

Comments
 (0)