Golang | Buscando un elemento de tipo int en rebanada de ints

El segmento de lenguaje de Go es más poderoso, flexible y conveniente que una array y es una estructura de datos liviana. El segmento es una secuencia de longitud variable que almacena elementos de un tipo similar, no está permitido almacenar diferentes tipos de elementos en el mismo segmento.
En el segmento Go, puede buscar un elemento de tipo int en el segmento dado de ints con la ayuda de SearchInts()función. Esta función busca el elemento dado en una porción ordenada de enteros y devuelve el índice de ese elemento si está presente en la porción dada. Y si el elemento dado no está disponible en el segmento (podría ser len(s_slice)), devuelve el índice para insertar el elemento en el segmento. El segmento especificado debe ordenarse en orden ascendente. Se define en el paquete de clasificación, por lo que debe importar el paquete de clasificación en su programa para acceder a la función SearchInts.

Sintaxis:

func SearchInts(s_slice []int, i int) int

Ejemplo 1:

// Go program to illustrate how to search
// an int type element in the slice of ints
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and searching an element 
    // in the given slice of ints
    // Using SearchInts function
    res1 := sort.SearchInts([]int{1, 2, 3,
                        4, 5, 6, 7, 8}, 5)
      
    res2 := sort.SearchInts([]int{200, 300, 
                 400, 500, 600, 700}, 400)
  
    // Displaying the results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
  
}

Producción:

Result 1:  4
Result 2:  2

Ejemplo 2:

// Go program to illustrate how to search an 
// element of int type in the slice of ints
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing
    // slice of ints using the 
    // shorthand declaration
    slice_1 := []int{34, 67, 78, 10, 43, 67, 8}
    slice_2 := []int{100, 500, 300, 600, 900, 1000}
  
    var f1, f2, f3 int
    f1 = 67
    f2 = 300
    f3 = 100
  
    // Sorting the given slice of ints
    sort.Ints(slice_1)
    sort.Ints(slice_2)
  
    // Displaying the slices
    fmt.Println("Slice 1: ", slice_1)
    fmt.Println("Slice 2: ", slice_2)
  
    // Searching a int type element 
    // in the given slice using 
    // the SearchInts function
    res1 := sort.SearchInts(slice_1, f1)
    res2 := sort.SearchInts(slice_2, f2)
    res3 := sort.SearchInts(slice_2, f3)
  
    // Displaying the results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
  
}

Producción:

Slice 1:  [8 10 34 43 67 67 78]
Slice 2:  [100 300 500 600 900 1000]
Result 1:  4
Result 2:  1
Result 3:  0

Publicación traducida automáticamente

Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *