Golang program to split a slice into two halves


A slice can also be called as a dynamic array as it's value is dynamic whereas normal array is static. This makes slice more efficient and faster. They are passed by reference instead by value. Let us learn how to split a slice into two halves using Golang program.

Syntax

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.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create slice and fill values inside the slice using append function which is a built-in function.

  • Step 3 − Print the slice on the console using fmt.Println() function.

  • Step 4 − Find the mid of slice using len(slice)/2 and store it inside the mid variable, here len refers to the length of slice.

  • Step 5 − In the left variable store the values before mid and equal to mid and in the right variable store values after mid.

  • Step 6 − Print the left half and right half on the console using fmt.Println() function where ln means new line.

Example 1

In this example we will see learn how to split a slice into two halves using middle value of slice. The output printed on the screen will be left and right slice with mid element included in left and excluded in right. Let’s learn via code and algorithm how to execute this.

package main
import "fmt"
func main() {
   var slice []int // initialize slice
   slice = append(slice, 10) //fill the slice using append function
   slice = append(slice, 20)
   slice = append(slice, 30)
   slice = append(slice, 40)
   fmt.Println("The original slice created is:", slice) //print original slice
   mid := len(slice) / 2 //find mid element
   left := slice[:mid] //store left slice elements in left variable
   right := slice[mid:] //store right slice elements in right variable
   fmt.Println("The left half presented here is:", left) //print left elements
   fmt.Println("The right half presented here is:", right) //print right elements
}

Output

The original slice created is: [10 20 30 40]
The left half presented here is: [10 20]
The right half presented here is: [30 40]

Example 2

In this example we will look how to split slice into two halves using append function which is a type of built-in function. It will be used to append elements from the second half to the new slice created inside the append function parameters. Here lets dive into the algorithm and the code to know more about this.

package main
import "fmt"
func main() {
   var slice []int // initialize slice
   slice = append(slice, 10) //fill the slice using append function
   slice = append(slice, 20)
   slice = append(slice, 30)
   slice = append(slice, 40)

   fmt.Println("The original slice created here is:", slice) //print original slice
   mid := len(slice) / 2 //find mid element
   left := slice[:mid] //
   right := append([]int{}, slice[mid:]...) //store the right elements into the right variable
   fmt.Println("The left half presented here is:", left) //print left elements
   fmt.Println("The right half presented here is:", right) //print right elements
}

Output

The original slice created here is: [10 20 30 40]
The left half presented here is: [10 20]
The right half presented here is: [30 40]

Conclusion

We executed the program of splitting a slice into two halves using two examples. In the first example we have calculated halves using middle value. In the second example we used append function to find halves. Additionally, we used one built-in function named append function which is used to add values to slice and create new slice named right slice. Both the examples give similar output. Hence, program executed successfully.

Updated on: 13-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements