Reverse an array using 5 different ways

1. Introduction

In this article we will see how to reverse the elements in a primitive array as well as in object array. We will explore several different ways to do this which will include external libraries too.

Embed from Getty Images

2. Different ways to reverse

2.1 Reverse int[] array using auxiliary space

Start iterating array from start of an array and copy values to another array in reverse by maintaining a reverse index. The resulting array with have elements in reverse order as compared to first array. Let’s say our original array is of length 1,000,000 i.e. 1 million. Then for this solution we are taking another auxiliary array of 1million length and copying values into it. This is not space efficient solution.

Time Complexity is O(n) as we are iterating entire array.

Space Complexity is O(n) as we are taking new array to store the result.

int[] arr = { 1, 2, 3, 4, 5 };
int[] result = new int[arr.length];
int index = arr.length - 1;
for (int i = 0; i < arr.length; i++) {
    result[index] = arr[i];
    index--;
}

Assert.assertArrayEquals(new int[] { 5, 4, 3, 2, 1 }, result);

2.2 Reverse int[] array without using auxiliary space

We can reverse the array in-place. How to do that? Well it is simple thing to do. Swap first and last element. When we will be halfway to array we would have reversed entire array. Pretty neat, fast and efficient solution.

Time Complexity is O(n) as we are iterating entire array.

Space Complexity is O(1) as we are not taking new array to store the result.

int[] arr = { 1, 2, 3, 4, 5 };
int start = 0;
int end = arr.length - 1;
while (start < end) {
    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
}

Assert.assertArrayEquals(new int[] { 5, 4, 3, 2, 1 }, arr);

2.3 Reverse int[] array using apache-commons

We can reverse an array using open source library called commons-lang3 of Apache. For this we can use class ArrayUtils and method reverse. Just pass the array as a parameter to reverse method and it will reverse the array in-place i.e. without using any auxiliary space.

int[] arr = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(arr);

Assert.assertArrayEquals(new int[] { 5, 4, 3, 2, 1 }, arr);

2.4 Reverse Object[] array using Collections.reverse(List)

We can reverse an Object[] using reverse method of Collections class. Collections class contains several utility methods. reverse(List) method accepts List as a parameter hence we need to convert Object[] to List using Arrays.asList() method. We just need to supply the parameter and it will reverse the elements in-place.

Integer[] arr = {1, 2, 3, 4, 5};
List<Integer> numbers = Arrays.asList(arr);
Collections.reverse(numbers);

Assert.assertEquals(Arrays.asList(5, 4, 3, 2, 1), numbers);

2.5 Reverse Object[] array using google-guava

We can reverse an Object[] using reverse method of Lists class of open source library called guava designed by Google. Lists class of guava library contains static factories and several utility methods and that can used to create new ArrayList and perform several operations of List.

Integer[] arr = {1, 2, 3, 4, 5};
List<Integer> numbers = Arrays.asList(arr);
numbers = Lists.reverse(numbers);

Assert.assertEquals(Arrays.asList(5, 4, 3, 2, 1), numbers);

3. Conclusion

In this article, we saw several different ways to reverse primitive and Object[] array using plan java methods as well as using third party library such as google guava and commons-lang3.

Leave a Reply

Your email address will not be published. Required fields are marked *