Programming problem: Concatenation of Array

Pre-allocate an array of double length, then fill both halves in a single pass — writing each element at index `i` and `i + n`. Runs in O(n) time with a clean, spread-free approach.

Programming problem: Concatenation of Array

Question

You are given an integer array nums of length n. Create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).

Specifically, ans is the concatenation of two nums arrays.

Return the array ans.

Example 1

Input: nums = [1,4,1,2]

Output: [1,4,1,2,1,4,1,2]

Example 2

Input: nums = [22,21,20,1]

Output: [22,21,20,1,22,21,20,1]

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000

Solution

function getConcatenation(nums: number[]) {
  const n = nums.length;
  const ans = new Array(n * 2);

  for (let i = 0; i < n; i++) {
    ans[i] = nums[i];
    ans[i + n] = nums[i];
  }

  return ans;
}