Skip to content

Instantly share code, notes, and snippets.

@nidhi-canopas
Created February 22, 2022 05:47
Show Gist options
  • Select an option

  • Save nidhi-canopas/c5c6e4357651854162617b994e13b25e to your computer and use it in GitHub Desktop.

Select an option

Save nidhi-canopas/c5c6e4357651854162617b994e13b25e to your computer and use it in GitHub Desktop.
import "fmt"
func main() {
a := []int{1, 2, 3, 4, 5, 6} // input int array
reverseArray := ReverseSlice(a)
fmt.Println("Reverted array : ", reverseArray) // print output
}
func ReverseSlice(a []int) []int {
for i := len(a)/2 - 1; i >= 0; i-- {
pos := len(a) - 1 - i
a[i], a[pos] = a[pos], a[i]
}
return a
}
output:
Reverted array : [6 5 4 3 2 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment