// Reverse returns a new slice with the elements of the input slice in reverse order.
func Reverse[T any](arr []T) []T {
n := len(arr)
reversed := make([]T, n) // Create a new slice of the same length
for i := 0; i < n; i++ {
reversed[i] = arr[n-1-i] // Copy elements from end to start
}
return reversed
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter