Reverse the array

Reverse the array

Reversing an array is the first step towards deepening your understanding of arrays as well as logical thinking. In this series, we will go slow initially for my noob audience. But soon we will pick up the pace as peeps get pro.

So let's begin with a simple approach.

Simple Approach

A simple way to reverse an array is to start by swapping the first and the last element of the array and continuing the swapping as we move towards the center of the array. An example might help to understand the process better.

Initial Array
[1, 2, 3, 4, 5, 6, 7]
Swap first and last elements
[7, 2, 3, 4, 5, 6, 1]
Move one element forward from the front and
one element back from the end the swap
[7, 6, 3, 4, 5, 2, 1]
Last step we swapped 2 & 6. Now we will swap 3 and 5.
[7, 6, 5, 4, 3, 2, 1]
We can stop as we have reached the center of the array.

Here is the code for the above approach:

function iterative_reverse(arr) {
  let res = arr;
  const n = res.length;

  for (let i = 0; i < n / 2; i++) {
    let temp = res[i];
    res[i] = res[n - i - 1];
    res[n - i - 1] = temp;
  }

  return res;
}

Input and Output

iterative_reverse([1, 2, 3, 4, 5, 6, 7]);
-> [7, 6, 5, 4, 3, 2, 1]

Explanation:

We needed to run a loop from 0 to the center of the array which is n/2.

We can access the ith element from the start with arr[i] and the ith element from the end with arr[n - i - 1] . The code inside the for loop simply swaps the two elements. You can try swapping two variables if you find this part a bit complex.

Recursive Approach

Next, we will reverse the array using recursion. I am assuming you know a bit of recursion for this solution. Let's think of what variables we might as function parameters. We would need the array arr itself, then we would need a pointer let's say i which can have a default value of 0 and finally we would need a variable to store the resultant array res. The default value of this array would be []. So our function would look something like this initially:

function recursive_reverse(arr, i = 0, res = []) {}

To solve a recursive problem, we need to come up with the terminating condition first. In this case, reaching the end of the array would be the terminating condition.

function recursive_reverse(arr, i = 0, res = []) {
  if (i >= arr.length) {
    return res;
  } 
}

And in the recursive call,

  • we can pass the arr without any change

  • we need to move the pointer to the next element.

  • and in the resultant array, we pass the current res array with arr[i] added to the end of the array.

function recursive_reverse(arr, i = 0, res = []) {
  if (i >= arr.length) {
    return res;
  }
  return recursive_reverse(arr, i + 1, [
    ...res,
    arr[i],
  ]);
}
recursive_reverse([1, 2, 3, 4, 5, 6, 7]);
-> [7, 6, 5, 4, 3, 2, 1]

Functional Approach

At times I try to write clean functional code that does the job in fewer lines of code. It is something that I enjoy doing and I don't recommend you to go out of your way to understand it. But just look at this beautiful piece of code.

function functional_reverse(arr) {
  return arr.reduce(
    (acc, curr) => [...acc, curr],
    []
  );
}
functional_reverse([1, 2, 3, 4, 5, 6, 7]);
-> [7, 6, 5, 4, 3, 2, 1]

This is it for this article. Thank you for reading the whole thing. Next time one dragon ball Z, we will find the maximum and minimum element of an array.