Exercise: Palindromic Arrays

Let's try to create an array where the elements make a palindromic sequence.

Problem Statement

A palindrome is a pattern which is symmetrical around a center point. Here’s an example:

aabbcbbaa

As we can see, there is a symmetrical pattern around c. We can do the same thing with an array. Here’s an example of a palindrome in an array:

[| 0, 1, 2, 1, 0 |]

In this exercise, you are given an array of size n. You must create a palindrome of that array.

As we can observe, the new array will be of length (n * 2) - 1.

Sample Input

inputArray1: [| 0, 1, 2, 3 |]

inputArray2: [| 40, 20, 20 |]

Sample Output

outputArray1: [| 0, 1, 2, 3, 2, 1, 0 |]

outputArray2: [| 40, 20, 20, 20, 40 |]

Coding Challenge

In the code below, there are four predefined arrays: inputArray1, inputArray2, outputArray1, and outputArray2. Your goal is to make outputArray1 and outputArray2 palindromes of inputArray1 and inputArray2, respectively.

Think carefully about which properties of an array will be required to solve this problem. Based on what we’ve studied so far, the solution shouldn’t be very difficult.

If you feel stuck, you can always refer to the solution review in the next lesson.

Good luck!

Get hands-on with 1400+ tech skills courses.