Golang Program to find the index of the first occurrence of the specified item in the array


In this article we will write a go language program to find the index of the first occurrence of the specified item in an array. we will use for loops and inbuilt go library functions to achieve the result.

Method 1: Using SearchInts() Library Function

Syntax

func Sort(data Interface)

The sort() function is present in sort package. this function is used to sort an array in ascending order. The array to be sorted is passed as an argument to the function. The time complexity of this function is O(n * logn).

type IntSlice []int

The IntSlice() function is present in sort package. this function is used to attach the method of interface to the array of int so that it could be sorted in increasing order.

func SearchInts(a []int, x int) int

The SearchInts() function is present in sort package. this function is used to search for a particular element in the array of integers. the function accepts the array along with the element to be searched as argument to the function and returns the index of that particular element as the result.

Algorithm

STEP 1 − First, we need to import the fmt and sort packages.

STEP 2 − Then, start the main() function. In the main() initialize an array of integers using make function and store elements to the array using append() function.

STEP 3 − Now, print the array on the screen and use IntSlice() and Sort() functions present in sort packages to sort the array in ascending order.

STEP 4 − Further, print the sorted array on the screen and store the element to be searched in a variable called elem.

STEP 5 − Now, in order to get the index of the above chosen element use the SearchInts() function of the sort package and pass the array along with the elem variable as arguments to the function.

STEP 6 − Store, the result returned by the function which is the index of first occurrence of that element in a variable and print it on the screen using fmt.Println() function.

In this example we will write a go language program to find the index of the first occurrence of an element in an array of integers using SearchInts() library function.

package main
import (
   "fmt"
   "sort"
)
func main() {

   // initializing an array
   array := make([]int, 0, 5)
   array = append(array, 74, 59, -784, 784, 59)
   a := sort.IntSlice(array[0:])
   sort.Sort(a)
   var elem int = 59
   fmt.Println("The array is:", a)
   pos := sort.SearchInts(a, elem)
   fmt.Println("The position of", elem, "in the array is:", pos)
}

Output

The array is: [-784 59 59 74 784]
The position of 59 in the array is: 1

Method 2: Using SearchString() Function

Syntax

type StringSlice []string

The StringSlice() function is present in sort package. this function is used to attach the method of interface to the array of string so that it could be sorted in increasing order.

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

The SearchStrings() function is present in sort package. this function is used to search for a particular element in the array of strings. the function accepts the array along with the element to be searched as an argument to the function and returns the index of that particular element as the result.

Algorithm

STEP 1 − First, we need to import the fmt and sort packages.

STEP 2 − Then, start the main() function. In the main() initialize an array of strings using make function and store elements to the array using append() function.

STEP 3 − Now, print the array on the screen and use StringSlice() and Sort() functions present in sort packages to sort the array in ascending order.

STEP 4 − Further, print the sorted array on the screen and store the element to be searched in a variable called elem.

STEP 5 − Now, in order to get the index of the above chosen element use the SearchString() function of the sort package and pass the array along with the elem variable as arguments to the function.

STEP 6 − Store, the result returned by the function which is the index of first occurrence of that element in a variable and print it on the screen using fmt.Println() function.

Example

In this example we will write a go language program to find the index of the first occurrence of an element in an array of integers using SearchStrings() library function.

package main
import (
   "fmt"
   "sort"
)
func main() {

   // initializing an array
   array := make([]string, 0, 7)
   array = append(array, "d", "c", "b", "y", "x", "a", "x")
   a := sort.StringSlice(array[0:])
   sort.Sort(a)
   var elem string = "x"
   fmt.Println("The array is:", a)
   pos := sort.SearchStrings(a, elem)
   fmt.Println("The position of", elem, "in the array is:", pos)
}

Output

The array is: [a b c d x x y]
The position of x in the array is: 4

Conclusion

We have successfully compiled a golang program to find the index of the first occurrence of the specified item in an array along with examples.

Updated on: 09-Feb-2023

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements