Concatenation of Array Visualizer & Step-by-Step Algorithm Solution

Given an integer array nums of length n, create and return an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n.

Category: arrays | Difficulty: Easy

Tags: Array, Simulation, Two Pointers

Concatenation of Array

nums (Input Array, n = 3)
1
0
2
1
1
2
ans (Concatenated Array, 2n = 6)
0
0
0
1
0
2
0
3
0
4
0
5
100%
state
nums.length3
Initialization
1/14
Explanation

Starting getConcatenation with nums = [1, 2, 1] of length n = 3.

Source Code
1function getConcatenation(nums: number[]): number[] {
2 const n = nums.length;
3 const ans = Array(n * 2).fill(0);
4 for (let i = 0; i < nums.length; i++) {
5 ans[i] = nums[i];
6 ans[i + n] = nums[i];
7 }
8 return ans;
9}