Answers for "how to reverse array golang"

Go
0

how to reverse array golang

//  string teritory
stringArray := strings.Split("you must be pray before learn anything", " ")
fmt.Println("before sort", stringArray)

fmt.Printf("data not sorted %v \n", sort.StringsAreSorted(stringArray))

sort.Sort(sort.Reverse(sort.StringSlice(stringArray)))
fmt.Println("after sort", stringArray)

fmt.Printf("data sorted %v \n", sort.StringsAreSorted(stringArray))

//  int teritory
intArray := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println("before sort", intArray)

fmt.Printf("data not sorted %v \n", sort.IntsAreSorted(intArray))

sort.Sort(sort.Reverse(sort.IntSlice(intArray)))
fmt.Println("after sort", intArray)

fmt.Printf("data sorted %v \n", sort.IntsAreSorted(intArray))
Posted by: Guest on October-04-2021
0

how to reverse array golang

package main
import "fmt"

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

func main(){
   fmt.Println(reverseArray([]int{1, 2, 3, 4, 5}))
   fmt.Println(reverseArray([]int{3, 5, 7, 2, 1}))
   fmt.Println(reverseArray([]int{9, 8, 6, 1, 0}))
}
Posted by: Guest on October-04-2021
0

how to reverse array golang

package main

 import "fmt"

 func main() {

         cities := [...]string{"New York", "Beijing", "Bangkok", "Adelaide", "Tokyo", "Seoul", "Zurich", "Seattle"}

         fmt.Println("Before : ", cities)

         reversed := []string{}

         // reverse order
         // and append into new slice
         for i := range cities {
                 n := cities[len(cities)-1-i]
                 //fmt.Println(n) -- sanity check
                 reversed = append(reversed, n)
         }

         fmt.Println("After : ", reversed)

 }
Posted by: Guest on October-04-2021

Browse Popular Code Answers by Language