Golang sort.SearchStrings() Function with Examples

Golang | sort.SearchStrings() Function: Here, we are going to learn about the SearchStrings() function of the sort package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 11, 2021

sort.SearchStrings()

The SearchStrings() function is an inbuilt function of the sort package which is used to search for the given element (x) in a sorted slice of strings and returns the index as specified by Search().

It accepts two parameters (a []string), x string)) – a is the sorted slice of string type and x is a string type element to search, and returns the index as specified by Search.

Note: The SearchStrings() result is the index to insert the elements (x), if x is not present (it could be len(a)). The slice must be sorted in ascending order.

Syntax:

func SearchStrings(a []string, x string) int

Parameter(s):

  • a : A sorted slice of strings.
  • x : A string type elements to be searched within a.

Return Value:

The return type of the SearchStrings() function is an int, it returns the index as specified by Search.

Example 1:

// Golang program to demonstrate the
// example of sort.SearchStrings() Function

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []string{"Apple", "Banana", "Coconut", "Dates"}

	x := "Banana"
	fmt.Println(sort.SearchStrings(a, x))

	x = "Coconut"
	fmt.Println(sort.SearchStrings(a, x))

	x = "Dates"
	fmt.Println(sort.SearchStrings(a, x))

	// items not found case
	x = "Manngo"
	fmt.Println(sort.SearchStrings(a, x))

	x = "Watermelon"
	fmt.Println(sort.SearchStrings(a, x))
}

Output:

1
2
3
4
4

Example 2:

// Golang program to demonstrate the
// example of sort.SearchStrings() Function

package main

import (
	"fmt"
	"sort"
)

func main() {
	a := []string{"Apple", "Banana", "Coconut", "Dates"}

	x := "Apple"
	i := sort.SearchStrings(a, x)
	fmt.Printf("Element %s found at index %d in %v\n", x, i, a)

	x = "Mango"
	i = sort.SearchStrings(a, x)
	fmt.Printf("Element %s not found, it can inserted at index %d in %v\n", x, i, a)

	x = "Watermelon"
	i = sort.SearchStrings(a, x)
	fmt.Printf("Element %s not found, it can inserted at index %d in %v\n", x, i, a)
}

Output:

Element Apple found at index 0 in [Apple Banana Coconut Dates]
Element Mango not found, it can inserted at index 4 in [Apple Banana Coconut Dates]
Element Watermelon not found, it can inserted at index 4 in [Apple Banana Coconut Dates]

Example 3:

// Golang program to demonstrate the
// example of sort.SearchStrings() Function

package main

import (
	"fmt"
	"sort"
)

// Main function
func main() {
	a := []string{"Dates", "Banana", "Coconut", "Apple"}

	b := []string{"Coconut", "Banana", "Mango", "Dates"}

	var x1, x2 string
	x1 = "Apple"
	x2 = "Dates"

	// Sorting  both slices of strings
	sort.Strings(a)
	sort.Strings(b)

	// Printing both slices of ints
	fmt.Println("a: ", a)
	fmt.Println("b: ", b)

	// Searching elements
	ind1 := sort.SearchStrings(a, x1)
	ind2 := sort.SearchStrings(b, x2)

	// Displaying the results
	fmt.Println("Result 1: ", ind1)
	fmt.Println("Result 2: ", ind2)
}

Output:

a:  [Apple Banana Coconut Dates]
b:  [Banana Coconut Dates Mango]
Result 1:  0
Result 2:  2

Golang sort Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.