Got confusion with a function
package main
import "fmt"
func dominantIndex(nums []int) int {
var max, max2 = -12423421, -12423421
var i, j = -1, -1
for k, num := range nums {
if num > max {
max, max2 = num, max
i, j = k, i
} else if num > max2 {
max2 = num
j = k
}
}
if max >= max2*2 {
return i
}
return -1
}
func main() {
var a = []int{3, 6, 100, 1, 0 }
fmt.Print(dominantIndex(a))
}
I have to insert a nonsense statement in the loop such as j = j
. Otherwise, it raises ./hello.go:7:6: j declared and not used
. Wonder if there is any fix.