I need to compare 2 arrays of uint32, something like this
func in(a uint32, list []uint32) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
for n := 0 ;n < len(a); n++ {
fmt.Println(in(a[n], b))
}
// a and b []uint32
but I think it is not the most optimal way
Why not just use ==
if you are actually using arrays?
https://golang.org/ref/spec#Comparison_operators
Array values are comparable if values of the array element type are comparable. Two array values are equal if their corresponding elements are equal.
If you are using slices, you can use reflect.DeepEqual
.
But, from your code, it seems like you should look into https://godoc.org/golang.org/x/tools/container/intsets
Then, you create your two intsets.Sparse
and could then do:
func main() {
s1 := intsets.Sparse{}
s2 := intsets.Sparse{}
s1.Insert(1)
s1.Insert(2)
s1.Insert(3)
s2.Insert(1)
s2.Insert(2)
//s1:{1,2,3}
//s2:{1,2}
fmt.Println(s1.SubsetOf(&s2), s2.SubsetOf(&s1))
//false, true
}
which will ignore duplicates, but let you know if s1 is a subset of s2, meaning every element in s1 exists in s2.