Golang program to rotate elements of a slice


A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover; they are passed by reference instead by value. Here will learn various techniques to rotate elements of a slice using go programming language.

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.

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.

func copy(dst, str[] type) int

The copy function in go language is used to copy the values of one source array to the destination array and returns the number of elements copied as the result. It takes the two arrays as an argument.

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

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 a main function and in that function create a slice and add elements to it using append function.

  • Step 3 − Print the slice on the console using print statement in Golang.

  • Step 4 − Call the rotate function from the main with two inputs inside it one is the slice which is to be rotated and second is the by no. of positions slice is rotated.

  • Step 5 − In the rotate function call the reverse function every time with different one time left, one time right and in the last a full slice.

  • Step 6 − In the reverse function use Golang multiple loop feature reverse the elements and the values in slice are reversed at particular positions.

  • Step 7 − After the slice is rotated print it on the console using fmt.Println() function where ln means new line.

Example 1

In this example, we will learn how to rotate a slice by reversing the slice at different positions and then reversing the entire slice.

package main
import (
   "fmt"
)
func rotate(slice []int, positions int) {
   positions = positions % len(slice) //find the position
   reverse(slice[:positions]) //reverse the beginning elements
   reverse(slice[positions:]) //reverse the end elements
   reverse(slice) //reverse the entire slice
}
func reverse(slice []int) {
   for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
      slice[i], slice[j] = slice[j], slice[i]
   }
}
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)
   slice = append(slice, 50)
   fmt.Println("The original slice created here is: ", slice)
   rotate(slice, 2) //call the rotate function
   fmt.Println("The rotated slice is: ", slice)
}

Output

The original slice created here is:  [10 20 30 40 50]
The rotated slice is:  [30 40 50 10 20]

Example 2

In this example we will learn how to rotate a slice using built-in copy function. Here in rotate function values will be copied by creating another temp variable. Let’s see the algorithm and the code to see how it’s done.

package main
import (
   "fmt"
)
func rotate(slice []int, position int) {
   position = position % len(slice) //find position
   temp := make([]int, position) //create a temp slice to store values
   copy(temp, slice[:position])
   copy(slice, slice[position:])
   copy(slice[len(slice)-position:], temp) //copy elements from temp to end of slice
}
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)
   slice = append(slice, 50)
   fmt.Println("The original slice created here is: ", slice)
   rotate(slice, 2)
   fmt.Println("The rotated slice is: ", slice) //print rotated slice
}

Output

The original slice created here is:  [10 20 30 40 50]
The rotated slice is:  [30 40 50 10 20]

Conclusion

We executed the program of rotating a slice using two examples. In the first example we used the reverse function to rotate the slice and in the second example we used copy function with an additional temporary variable. Hence, program executed successfully.

Updated on: 13-Feb-2023

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements