Created
August 7, 2019 14:57
-
-
Save parzibyte/5a1ee25e1dde6d5b175d2b871ff07f38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Comprobar si elemento existe dentro de un arreglo | |
en Go | |
https://parzibyte.me/blog | |
*/ | |
package main | |
import "fmt" | |
func main() { | |
arreglo := []int{1, 60, 33, 45, 500} | |
busqueda := 50 | |
existe := existeEnArreglo(arreglo, busqueda) | |
fmt.Printf("Buscando %d en arreglo. ¿Existe? %t\n", busqueda, existe) | |
// Otra búsqueda | |
busqueda = 45 | |
existe = existeEnArreglo(arreglo, busqueda) | |
fmt.Printf("Buscando %d en arreglo. ¿Existe? %t", busqueda, existe) | |
} | |
func existeEnArreglo(arreglo []int, busqueda int) bool { | |
for _, numero := range arreglo { | |
if numero == busqueda { | |
return true | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment