check if element exists in array golang

Solutions on MaxInterview for check if element exists in array golang by the best coders in the world

showing results for - "check if element exists in array golang"
Eren
29 Jan 2020
1func stringInSlice(a string, list []string) bool {
2    for _, b := range list {
3        if b == a {
4            return true
5        }
6    }
7    return false
8}
Marcus
28 Oct 2020
1Mostafa has already pointed out that such a method is trivial to write, and mkb gave you a hint to use the binary search from the sort package. But if you are going to do a lot of such contains checks, you might also consider using a map instead.
2
3It's trivial to check if a specific map key exists by using the value, ok := yourmap[key] idiom. Since you aren't interested in the value, you might also create a map[string]struct{} for example. Using an empty struct{} here has the advantage that it doesn't require any additional space and Go's internal map type is optimized for that kind of values. Therefore, map[string] struct{} is a popular choice for sets in the Go world.
queries leading to this page
golang check if array values existsif in array golanghow to check array contains value golangcheck istring in array golangcheck in array golangcheck array element exist golangcheck value exists in array golanghow to check if string exists in array golangcheck if value in array golanggolang check value in arraycheck array index exist golanghow to check if a value is present in array in golangcheck if value is present in array golanggolang check if item exist in arraygolang array value existscheck element in array golangcheck if value is in array golanghgo check if array containscheck exist in array golanggo check if element in arraycheck an element exists in array golangcheck if element exists in array golanggolang check if array containshow to check if array index exists or not in golangif array contains golangif id in array golangcheck if value exists in array golangcheck in array gogolang check array contains valuegolang check if array index existscheck if string exists in array golanggolang check if array element existshow to check if array contains a string in golanggolang check if string exists in arraygolang check if exist in arraygolang check element is in arraygolang check if array contains elementgo check element in arrayhow to check array index exists in golangin array check for golanggolang check if an array element existsgolang check item exist in arraygolang check if value in arraygolang check if all elements exist in arraygolang is in arrayhow to check a string exist in array golanggolang check if array existknow if element in array golanggolang check if item in arraygolang contains in arraycheck array includes golangcheck if element resides in array golanghow to check if it is present in array in golangcheck value is in array golangcheck if element existing in array golanggolang check if in arraygolang check if elements in array exists in other listcheck if item in array golanggolang if array containsbest way to check in an array golanggolang check if value exists in arraygolang check if string in arraycheck in array in golangcheck array has values golanggolang check if item exists in arraygolang array containsgo check if string in arraycheck if array contains value golangcheck value in array golanggolang check if not in arraycheck if array contains all elements from anather array golanggolang array contain valuegolang check array containscheck if in array golanghow to check if a value is present inside a array in golanggolang check if exists in arraycheck if array element exists golanghow to check array contains value in golanggolang check if value is in arraycheck if element exists in array golang