How do I reverse an array in Go?

Go

Go Problem Overview


http://play.golang.org/p/W70J4GU7nA

  s := []int{5, 2, 6, 3, 1, 4}
  sort.Reverse(sort.IntSlice(s))
  fmt.Println(s)
  // 5, 2, 6, 3, 1, 4

It is hard to understand what it means in func Reverse(data Interface) Interface .

How do I reverse an array? I do not need to sort.

Go Solutions


Solution 1 - Go

Honestly this one is simple enough that I'd just write it out like this:

package main

import "fmt"

func main() {

	s := []int{5, 2, 6, 3, 1, 4}

	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}

	fmt.Println(s)
}

http://play.golang.org/p/vkJg_D1yUb

(The other answers do a good job of explaining sort.Interface and how to use it; so I won't repeat that.)

Solution 2 - Go

Normally, to sort an array of integers you wrap them in an IntSlice, which defines the methods Len, Less, and Swap. These methods are in turn used by sort.Sort. What sort.Reverse does is that it takes an existing type that defines Len, Less, and Swap, but it replaces the Less method with a new one that is always the inverse of the underlying Less:

type reverse struct {
	// This embedded Interface permits Reverse to use the methods of
	// another Interface implementation.
	Interface
}

// Less returns the opposite of the embedded implementation's Less method.
func (r reverse) Less(i, j int) bool {
	return r.Interface.Less(j, i)
}

// Reverse returns the reverse order for data.
func Reverse(data Interface) Interface {
	return &reverse{data}
}

So when you write sort.Reverse(sort.IntSlice(s)), whats happening is that you're getting this new, 'modified' IntSlice that has it's Less method replaced. So if you call sort.Sort on it, which calls Less, it will get sorted in decreasing order.

Solution 3 - Go

I'm 2 years late, but just for fun and interest I'd like to contribute an "oddball" solution.

Assuming the task really is to reverse a list, then for raw performance bgp's solution is probably unbeatable. It gets the job done simply and effectively by swapping array items front to back, an operation that's efficient in the random-access structure of arrays and slices.

In Functional Programming languages, the idiomatic approach would often involve recursion. This looks a bit strange in Go and will have atrocious performance. That said, here's a recursive array reversal function (in a little test program):

package main

import (
	"fmt"
)

func main() {
	myInts := []int{ 8, 6, 7, 5, 3, 0, 9 }
	fmt.Printf("Ints %v reversed: %v\n", myInts, reverseInts(myInts))
}

func reverseInts(input []int) []int {
	if len(input) == 0 {
		return input
	}
	return append(reverseInts(input[1:]), input[0]) 
}

Output:

Ints [8 6 7 5 3 0 9] reversed: [9 0 3 5 7 6 8]

Again, this is for fun and not production. Not only is it slow, but it will overflow the stack if the list is too large. I just tested, and it will reverse a list of 1 million ints but crashes on 10 million.

Solution 4 - Go

First of all, if you want to reverse the array, do like this,

for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
    a[i], a[j] = a[j], a[i]
}

Then, look at the usage of Reverse in golang.org

package main

import (
    "fmt"
    "sort"
)

func main() {
    s := []int{5, 2, 6, 3, 1, 4} // unsorted
    sort.Sort(sort.Reverse(sort.IntSlice(s)))
    fmt.Println(s)
}

// output
// [6 5 4 3 2 1]

And look at the description of Reverse and Sort

func Reverse(data Interface) Interface
func Sort(data Interface)

> Sort sorts data. It makes one call to data.Len to determine n, and O(n*log(n)) calls to data.Less and data.Swap. The sort is not guaranteed to be stable.

So, as you know, Sort is not just a sort algorithm, you can view it as a factory, when you use Reverse it just return a reversed sort algorithm, Sort is just doing the sorting.

Solution 5 - Go

This is a more generic slice reverse function. It will panic if input is not a slice.

//panic if s is not a slice
func ReverseSlice(s interface{}) {
	size := reflect.ValueOf(s).Len()
	swap := reflect.Swapper(s)
	for i, j := 0, size-1; i < j; i, j = i+1, j-1 {
		swap(i, j)
	}
}

Solution 6 - Go

If you want to reverse the array, you can just go through it in reverse order. Since there is no "reverse range" primitive in the language (at least not yet), you must do something like this (http://play.golang.org/p/AhvAfMjs_7):

s := []int{5, 2, 6, 3, 1, 4}
for i := len(s) - 1; i >= 0; i-- {
	fmt.Print(s[i])
	if i > 0 {
		fmt.Print(", ")
	}
}
fmt.Println()

Regarding whether it is hard to understand what sort.Reverse(data Interface) Interface does, I thought the same until I saw the source code from "http://golang.org/src/pkg/sort/sort.go";.

It just makes the comparisons required for the sorting to be made "the other way around".

Solution 7 - Go

Here is a simple Go solution that uses an efficient (no extra memory) approach to reverse an array:

i := 0
j := len(nums) - 1
for i < j {
    nums[i], nums[j] = nums[j], nums[i]
    i++
    j--
}

The idea is that reversing an array is equivalent to swapping each element with its mirror image across the center.

https://play.golang.org/p/kLFpom4LH0g

Solution 8 - Go

Here is another way to do it

func main() {
	example := []int{1, 25, 3, 5, 4}
	sort.SliceStable(example, func(i, j int) bool {
		return true
	})
	fmt.Println(example)
}

https://play.golang.org/p/-tIzPX2Ds9z

Solution 9 - Go

func Reverse(data Interface) Interface

This means that it takes a sort.Interface and returns another sort.Interface -- it doesn't actually doing any sorting itself. For example, if you pass in sort.IntSlice (which is essentially a []int that can be passed to sort.Sort to sort it in ascending order) you'll get a new sort.Interface which sorts the ints in descending order instead.

By the way, if you click on the function name in the documentation, it links directly to the source for Reverse. As you can see, it just wraps the sort.Interface that you pass in, so the value returned from Reverse gets all the methods of the original sort.Interface. The only method that's different is the Less method which returns the opposite of the Less method on the embedded sort.Interface. See this part of the language spec for details on embedded fields.

Solution 10 - Go

From Golang wiki SliceTricks:

> To replace the contents of a slice with the same elements but in > reverse order: > > for i := len(a)/2-1; i >= 0; i-- { > opp := len(a)-1-i > a[i], a[opp] = a[opp], a[i] > } > > The same thing, except with two indices: > > for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 { > a[left], a[right] = a[right], a[left] > }

Solution 11 - Go

Here is a method using append:

package main
import "fmt"

func main() {
   a := []int{10, 20, 30, 40, 50}
   for n := len(a) - 2; n >= 0; n-- {
      a = append(a[:n], append(a[n + 1:], a[n])...)
   }
   fmt.Println(a)
}

Drawing of the steps:

10 20 30 40 50
10 20 30    50 40
10 20       50 40 30
10          50 40 30 20
            50 40 30 20 10

Solution 12 - Go

To reverse an array in place, iterate to its mid-point, and swap each element with its "mirror element":

func main() {
	xs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	itemCount := len(xs)
	for i := 0; i < itemCount/2; i++ {
		mirrorIdx := itemCount - i -1
		xs[i], xs[mirrorIdx] = xs[mirrorIdx], xs[i]
	}
	fmt.Printf("xs: %v\n", xs)
}

https://play.golang.org/p/JeSApt80_k

Solution 13 - Go

This answer is mainly for those beginners who wish to write this code using only one variable in the for loop instead of using two variables (like i & j).

package main

import "fmt"

func main() {
array := []int{45, 17, 43, 67, 21, 4, 97, 44, 54, 98, 665}
fmt.Println("initial array:", array)
loop_iteration := len(array)
if len(array)%2 == 0 {
	loop_iteration = (len(array) / 2) - 1
  } else {
	loop_iteration = int(len(array) / 2)  //This will give the lower integer value of that float number.
    }
for i := 0; i <= loop_iteration; i++ {
	array[i], array[(len(array)-1)-i] = array[(len(array)-1)-i], array[i]
    }
fmt.Println("reverse array:", array)
}

Solution 14 - Go

https://go.dev/play/p/bVp0x7v6Kbs

package main

import (
	"fmt"
)

func main() {
	arr := []int{1, 2, 3, 4, 5}
	fmt.Println(reverseArray(arr))
}

func reverseArray(arr []int) []int {
	reversed := make([]int, len(arr))
	j := 0
	for i := len(arr) - 1; i >= 0; i-- {
		reversed[j] = arr[i]
		j++
	}
	return reversed
}

Solution 15 - Go

Here is my solution.

package main

import (
	"fmt"
)

func main() {
	var numbers = [10]int {1,2,3,4,5,6,7,8,9,10}
	var reverseNumbers [10]int
	j:=0
	for i:=len(numbers)-1; i>=0 ; i-- {
		reverseNumbers[j]=numbers[i]
		j++	
	}
	fmt.Println(reverseNumbers)
}

Solution 16 - Go

Here is my solution to reversing an array:

func reverse_array(array []string) []string {
	lenx := len(array) // lenx holds the original array length
	reversed_array := make([]string, lenx) // creates a slice that refer to a new array of length lenx
	
	for i := 0; i < lenx; i++ {
		j := lenx - (i + 1) // j initially holds (lenx - 1) and decreases to 0 while i initially holds 0 and increase to (lenx - 1)
		reversed_array[i] = array[j]
	}
	
	return reversed_array
}

You can try this solution on the go playground the go playground

package main

import "fmt"

func main() {
    array := []string{"a", "b", "c", "d"}
	
	fmt.Println(reverse_array(array)) // prints [d c b a]
}

Solution 17 - Go

Do not reverse it, leave it as now and then just iterate it backwards.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2671513View Question on Stackoverflow
Solution 1 - GoBrad PeabodyView Answer on Stackoverflow
Solution 2 - GoMuhammad FaizanView Answer on Stackoverflow
Solution 3 - GoCarl SmotriczView Answer on Stackoverflow
Solution 4 - GoxiaoyusilenView Answer on Stackoverflow
Solution 5 - GoYongkeView Answer on Stackoverflow
Solution 6 - GoBorja BolillaView Answer on Stackoverflow
Solution 7 - GoPranjal MittalView Answer on Stackoverflow
Solution 8 - Gomanugupt1View Answer on Stackoverflow
Solution 9 - GoTylerView Answer on Stackoverflow
Solution 10 - GocakrawwView Answer on Stackoverflow
Solution 11 - GoZomboView Answer on Stackoverflow
Solution 12 - GoMartin ProbstView Answer on Stackoverflow
Solution 13 - GoCHANDAN GUPTAView Answer on Stackoverflow
Solution 14 - GoSamView Answer on Stackoverflow
Solution 15 - GoAkshay PunnathView Answer on Stackoverflow
Solution 16 - GodoncredasView Answer on Stackoverflow
Solution 17 - GoMarioView Answer on Stackoverflow