Golang | Searching an element of int type in slice of ints

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice.
In the Go slice, you can search an element of int type in the given slice of ints with the help of SearchInts() function. This function searches for the given element in a sorted slice of ints and returns the index of that element if present in the given slice. And if the given element is not available in the slice(it could be len(s_slice)), then it returns the index to insert the element in the slice. The specified slice must be sorted in ascending order. It is defined under the sort package so, you have to import sort package in your program for accessing SearchInts function.

Syntax:

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

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
}

chevron_right


Output:

Result 1:  4
Result 2:  2

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
  
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.