-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (39 loc) · 895 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"sort"
)
func main() {
dataset := [][]int{
{4, 3, 1, 3, 6}, // 2
{2, 3, 4, 6, 7, 2, 10}, // 1
{1, 2, 3, 4, 5, 6, 8, 7}, // 9
}
for _, d := range dataset {
fmt.Println(d, ":", firstMissingPositive(d))
}
}
// firstMissingPositive returns the smallest positive
// that is missing from the input slice 'nums'
func firstMissingPositive(nums []int) int {
// sort the input slice in ascending order
sort.Ints(nums)
// initialize the smallest missing
// positive integer with 1
c := 1
// iterate over the sorted slice
for _, num := range nums {
// if the current number is greater
// than 'c', we have found the
// first missing positive integer
if num > c {
break
}
// if the current number is equal
// to 'c', increment 'c' by 1
if num == c {
c++
}
}
return c
}