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 all 0 ≤ i < n. Return ans.
nums = [1,2,1][1,2,1,1,2,1]ans[2*n]nums[i] → ans[i] (first half)nums[i] → ans[i+n] (second half)The result is simply nums repeated twice. We write each element to its original position i and also to position i+n (offset by the array length). One pass, no extra logic needed.
You could append the array to a list twice, but that hides what the indexes are doing. The cleaner way is to understand where each value lands.
Every value at index i is copied to two places. Once at index i, and once at index i plus n.
A copy shifted by n lands at index i plus n. One loop can fill two positions at once.
Run the animation above on your own input, or watch the 1:10 video explanation of Concatenation of Array with a dry run and Java code.