Golang Program to Rotate Elements of an Array


In this tutorial, we will see to write a go language program to rotate an array. We will write two programs for this. One to rotate the array to left and another to rotate it to right.

Algorithm

STEP 1 − Import the fmt package that allows us to print anything on the screen.

STEP 2 − Create a function named rotateLeft() which returns the final array after rotating it.

STEP 3 − This function uses a for loop to iterate over the array and calls rotateLeftByOne() function in each iteration.

STEP 4 − This function takes the array as an argument and uses for loop to iterate over the array variable.

STEP 5 − In each iteration, we are placing the next value in the previous position and restore the first element.

STEP 6 − Now call the main() function.

STEP 7 − Initialize an array of integers and assign values to it.

STEP 8 − Print the array on the screen and call the rotateLeft() function by passing the array and the number of times the elements should be shifted as arguments to the function.

STEP 9 −Print the final array on the screen using fmt.Println() function.

Rotate an Array to Left

Example

The following code illustrates how we can rotate an array to left using a user-defined function.

package main
import "fmt"

// making a function to rotate elements of array
func rotateLeft(arr []int, count int) {
   for i := 0; i < count; i++ {
      rotateLeftByOne(arr)
   }
}
func rotateLeftByOne(arr []int) {
       var i int = 0
   var temp int = arr[0]
   for ; i < len(arr)-1; i++ {
      arr[i] = arr[i+1]
   }
   arr[i] = temp
}
func main() {
   arr := []int{1, 2, 3, 4, 5, 6, 7, 8}
   fmt.Println("The entered array is:", arr)
   rotateLeft(arr, 3)
   fmt.Println("The array obtained by rotating it to left by 3 positions is:", arr)
}

Output

The entered array is: [1 2 3 4 5 6 7 8]
The array obtained by rotating it to left by 3 positions is: [4 5 6 7 8 1 2 3]

Rotate an Array to Right

Example

The following code illustrates a go language program to rotate the array elements to right by any number of times.

package main
import "fmt"
func rotateRight(arr []int, count int) {
   for i := 0; i < count; i++ {
      var j, last int
      length := len(arr)
      last = arr[length-1]
      for j = length - 1; j > 0; j-- {
         arr[j] = arr[j-1]
      }
      arr[0] = last
   }
}
func main() {
   arr := []int{1, 2, 3, 4, 5, 6, 7, 8}
   fmt.Println("The entered array is:", arr)
   rotateRight(arr, 3)
   fmt.Println("The array obtained by rotating it to right by 3 positions is:", arr)
}

Output

The entered array is: [1 2 3 4 5 6 7 8]
The array obtained by rotating it to right by 3 positions is: [6 7 8 1 2 3 4 5]

Rotate Array to Right by using Internal Function

Syntax

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

Example

Let us now look at another program using which we can rotate the array variables to the right using the internal function

package main
import "fmt"
func rotateRight(nums []int, k int) {
   k %= len(nums)
   new_array := make([]int, len(nums))
   copy(new_array[:k], nums[len(nums)-k:])
   copy(new_array[k:], nums[:len(nums)-k])
   copy(nums, new_array)
}
func main() {
   arr := []int{1, 2, 3, 4, 5, 6, 7, 8}
   fmt.Println("The entered array is:", arr)
   rotateRight(arr, 4)
   fmt.Println("The array obtained by rotating it to right by 4 positions is:", arr)
}

Output

The entered array is: [1 2 3 4 5 6 7 8]
The array obtained by rotating it to right by 4 positions is: [5 6 7 8 1 2 3 4]

Conclusion

We have successfully compiled and executed a go language program to rotate the elements of an array along with examples.

Updated on: 10-Feb-2023

384 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements