Hi, I’m extremely new to Go and am wondering if I could get a pointer here. I was working on solving a problem on codefights, I solved the problem to the extent of getting correct answers, I can’t figure out how to make it execute under 4 seconds. I’m guessing there is a better way than nesting loops?
Link to Go Playground and shown code below. If anything stands out that should be done differently I’d love to know that as well.
Go Playground: https://play.golang.org/p/AT1EllmUhMI
package main
import (
"fmt"
)
//find the number that is duplicated first in the array
func firstDuplicate(a []int) int {
s := []int{}
for _, fVal := range a {
for _, val := range s {
if val == fVal {
return val
}
}
s = append(s, fVal)
}
return -1
}
func main() {
c := []int{2, 3, 3, 1, 5, 2} //returns 3
d := []int{1, 1, 2, 2, 1} //returns 1
fmt.Println(firstDuplicate(c))
fmt.Println(firstDuplicate(d))
}