Golang Program to get the subarray from an array using a specified range of indices


In this tutorial, we will write a go language program to get the sub array from an array using a specified range of indices. A subarray is a contiguous portion of an array, specified by a range of indices. For example, given the array [1, 2, 3, 4, 5] and the range of indices [1, 3], the resulting subarray would be [2, 3, 4].

Syntax

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments

func append(slice, element_1, element_2…, element_N) []T

The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. The function then returns the final slice of array containing all the values.

Method 1: Using User-Defined Function Outside Main Function()

The function that we will create will accept the array along with the start and end index and returns the final sub array that we can store and print on the screen.

Algorithm

STEP 1 − First, we need to import the fmt package.

STEP 2 − Then, we need to create a function that will get us the sub arrays. This function accept an array as an argument and returns the final sub array.

STEP 3 − inside this function create an empty sub array. use for loop to iterate over the array and store the elements between start and end indexes in the subarray created above.

STEP 4 − Return the final sub array.

STEP 5 − Create the main() function. Initialize an array and store elements to it. further print this array on the screen using fmt.Println() function.

STEP 6 − Now, store the start and end indexes to a variable and call the getSubArray() function by passing the array along with indexes as arguments to the function.

STEP 7 − Now, store the resultant array obtained from the function and print it on the screen.

Example

In this example we will use a user defined function to get a subarray from an array using a user defined external function.

package main
import "fmt"
// function to get a sub array
func getSubArray(arr []int, startIndex int, endIndex int) [3]int {
   
   // Initialize the subarray
   subArr := [3]int{}
   
   // Iterate through the specified range of indices
   for i := startIndex; i <= endIndex; i++ {
      
      // Add the element at the current index to the subarray
      subArr[i-startIndex] = arr[i]
   }
   return subArr
}
func main() {

   // Initialize the array
   array := make([]int, 0, 5)
   array = append(array, 1, 2, 3, 4, 5)
   fmt.Println("The given array is:", array)

   // Specify the range of indices
   startIndex := 1
   endIndex := 3

   // Getting the subarray
   subArr := getSubArray(array, startIndex, endIndex)

   // Print the subarray
   fmt.Println("The sub array obtained from the above array from index", startIndex, "to", endIndex, "is:", subArr)
}

Output

The given array is: [1 2 3 4 5]
The sub array obtained from the above array from index 1 to 3 is: [2 3 4]

Method 2: Using User-Defined Function Inside Main function()

Algorithm

STEP 1 − First, we need to import the fmt package.

STEP 2 − Create the main() function. Initialize an array and store elements to it. further print this array on the screen using fmt.Println() function.

STEP 3 − Now, store the start and end indexes to a variable and use these two variables as start and end

indexes around the array variable and store the result of sub array obtained in a variable.

STEP 4 − Finally, print the sub array obtained on the screen using fmt.Println() function.

Example

Golang program to get the sub array from an array of integers using a specified range of indeces using internal funcitons

package main
import "fmt"
func main() {

   // Initialize the array
   array := make([]int, 0, 5)
   array = append(array, 12, 21, 33, 47, 56)
   fmt.Println("The given array is:", array)

   // Specify the range of indices
   startIndex := 0
   endIndex := 2

   // Get the subarray using the copy function
   subArr := array[startIndex : endIndex+1]

   // Print the subarray
   fmt.Println("The sub array obtained from the above array from index", startIndex, "to", endIndex, "is:", subArr)
}

Output

The given array is: [12 21 33 47 56]
The sub array obtained from the above array from index 0 to 2 is: [12 21 33]

Conclusion

We have successfully compiled and executed a go language program to to get the sub array from an array using a specified range of indices. We have used two programs in here. In the first program we are using a user defined external function while in the second one we are using the internal library functions to achieve the results.

Updated on: 10-Feb-2023

884 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements