Given an integer array nums of length n, you want to 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.

Input: nums = [1, 2, 1]
Output: [1, 2, 1, 1, 2, 1]
Explanation: After copying nums array into twice, it looks like this.
[nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
[1, 2, 1, 1, 2, 1]
Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]

Contents

In this approach, we are going to create ans array with 2n size and then copy elements from nums array using below logic: ans[i] = nums[i]; ans[i+nums.length] = nums[i]; So, when we copy nums[i] to ans array, we copy to two target indexes, that is to ith index and to i + nums.lengthth index.

import java.util.Arrays; public class ConcatenationOfArray { static int[] getConcatenation(int[] nums) { int[] ans = new int[nums.length *2]; for(int i=0;i<nums.length; i++) { ans[i] = nums[i]; ans[i+nums.length] = nums[i]; } return ans; } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of nums array, this is because we are looping through nums array.
Space complexity: O(n) since we have to return ans array with 2n size.

Above implementations source code can be found at GitHub link for Java code