DEV Community

loading...

Reverse Array

niharikapujari profile image Niharika Pujari ・1 min read

Problem:

Write two functions reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as an argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse method.

Method 1: ReverseArray

Alt Text

Method 2: ReverseArrayInPlace

Alt Text

Which one is better? ReverseArrayInPlace : ReverseArray

ReverseArrayInPlace takes half the time and will replace two values at a time instead of looping the whole array and replacing the values one at a time.

Discussion (0)

Forem Open with the Forem app