Reverse The Array is a Data Structure problem which already asked in most of the interviews. In this tutorial we will learn about it with some examples in different-different languages. See below.

What it means by Reverse the array?
You already know about the “Reverse” it means opposite of that thing for which we want to reverse. Reversing an array mean the last item should be in first position and the element which are just before the last element should be after that is the reversing process.
How to Reverse the Array
We can reverse the Array using following some methods which are below.
- By inbuilt functions
- By swapping
- And Recursively calling a function
- Using an external array.
1. By Inbuilt functionality
Mostly all the programming languages provides the inbuilt functions to reverse an array. The below example is in JavaScript.
function reverse(arr){
arr.reverse();
console.log(arr)
}
var numbers = [1,2, 3, 4, 5]
reverse(numbers)
// output : [ 5, 4, 3, 2, 1 ]
2. By Swapping
We need to swipe two elements by each other using while condition like in the below image.
Explanation:
1. Create a temp variable and assign the first variable in it.
2. Now put the last index’s element in the first position.
3. And put the temp variable’s value into the last position.

Then we will get the following reversed array as.

Code Representation
Java
import java.util.Arrays;
public class Reverse {
static void reverseArray(int arr[]){
int first = 0;
int last = arr.length-1;
while(first < last){
int temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
first++;
last--;
}
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
int[] array2 = {4, 5, 6};
reverseArray(array2);
}
}
Answer: [6, 5, 4]
JavaScript
Below is the example by Swapping process in JavaScript.
function reverseArray(arr){
var first = 0;
var last = arr.length-1;
while(first < last){
var temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
first++;
last--;
}
console.log(arr)
}
var array = [1, 2, 3, 4, 5]
reverseArray(array)
// Answer: [ 5, 4, 3, 2, 1 ]
3. Reverse The Array Using Recursion
Explanation: Recursively call the function and pass the start and end value by decreasing the end value and increasing the start value. So, when the function calls itself again the start value will be increased, and the end value will be decreased.
Java: The below code is in Java.
class Test {
static void rvereseAnArray(int arr[], int start, int end)
{
int temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
rvereseAnArray(arr, start+1, end-1);
}
static void printArray(int arr[], int size)
{
for (int i=0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
printArray(arr, arr.length);
rvereseAnArray(arr, 0, arr.length-1);
System.out.println("Reversed array is = ");
printArray(arr, arr.length);
}
}
// Answer :
1 2 3 4 5 6 7 8
Reversed array is =
8 7 6 5 4 3 2 1
JavaScript
function rvereseAnArray(arr, start, end) {
var temp;
if (start >= end)
return;
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
rvereseAnArray(arr, start + 1, end - 1);
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8]
rvereseAnArray(arr, 0, arr.length-1)
console.log("Reversed array = ");
console.log(arr);
// Answer : Reversed array =
[ 8, 7, 6, 5, 4, 3, 2, 1]
4. Using an External Array
- Create an external array.
- Start the for loop from behind.
Java
import java.util.*;
class ReverseArray {
public static List<Integer> reverseAnArray(Integer arr[]) {
// create a new ArrayList
List<Integer> arrlist = new ArrayList<Integer>();
for (int i = arr.length; i > 0; i--) {
arrlist.add(i);
}
// return the arraylist
return arrlist;
}
public static void main(String[] args) {
Integer arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// get the arraylist
List<Integer> resultArray = reverseAnArray(arr);
// print the updated array
System.out.println("\nReversed array :\n" + resultArray);
}
}
//Answer:
// Reversed array :
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
JavaScript
function rvereseAnArray(arr) {
var temp = [];
for(let i=arr.length-1; i>0; i--){
temp.push(arr[i])
}
console.log("Reversed array = ");
console.log(temp);
}
var arr = [1, 2, 3, 4, 5, 6, 7, 8]
rvereseAnArray(arr)
// Answer : Reversed array =
[ 8, 7, 6, 5, 4, 3, 2, 1]
See more Data Structure problem from Here which can help you to fight any type of developer interview. You can also see more about it from Here also.
Thank you for reaching out this tutorial. You can comment in the comments section below for any doubt or query.