Concatenation of Array

easy array

Problem

Given nums of length n, return ans of length 2n where ans[i] == ans[i+n] == nums[i] for 0 ≤ i < n.

Inputnums = [1,2,1]
Output[1,2,1,1,2,1]
Just concatenate nums with itself.

def get_concatenation(nums):
    n = len(nums)
    ans = [0] * (2 * n)
    for i in range(n):
        ans[i] = ans[i + n] = nums[i]
    return ans
function getConcatenation(nums) {
  const n = nums.length, ans = new Array(2 * n);
  for (let i = 0; i < n; i++) ans[i] = ans[i + n] = nums[i];
  return ans;
}
class Solution {
    public int[] getConcatenation(int[] nums) {
        int n = nums.length;
        int[] ans = new int[2 * n];
        for (int i = 0; i < n; i++) ans[i] = ans[i + n] = nums[i];
        return ans;
    }
}
vector<int> getConcatenation(vector<int>& nums) {
    int n = nums.size();
    vector<int> ans(2 * n);
    for (int i = 0; i < n; i++) ans[i] = ans[i + n] = nums[i];
    return ans;
}
Time: O(n) Space: O(n)