- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to reverse an array
Examples
- Input arr = [3, 5, 7, 9, 10, 4] => [4, 10, 9, 7, 5, 3]
- Input arr = [1, 2, 3, 4, 5] => [5, 4, 3, 2, 1]
Approach to solve this problem
- Step 1: Define a function that accepts an array.
- Step 2: Start iterating the input array.
- Step 3: Swap first element with last element of the given array.
- Step 4: Return array.
Program
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})) }
Output
[5 4 3 2 1] [1 2 7 5 3] [0 1 6 8 9]
- Related Articles
- Write a program to reverse an array in JavaScript?
- Write a Golang program to reverse a string
- Write a Golang program to reverse a number
- Write a C program to reverse array
- Write a program to reverse an array or string in C++
- Write a Golang program to search an element in an array
- Write a Golang program to sort an array using Bubble Sort
- Write a Golang program to search an element in a sorted array
- Java program to reverse an array
- C# program to reverse an array
- Write a Golang program to find the frequency of an element in an array
- Golang program to reverse a slice
- Golang program to reverse a stack
- C program to reverse an array elements
- Write a Golang program to find the frequency of each element in an array

Advertisements