Created
February 22, 2022 05:47
-
-
Save nidhi-canopas/c5c6e4357651854162617b994e13b25e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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