← Back to DSA Animator
Concatenation of Array LC #1929 Easy Array Basics
Problem

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.

Example 1
Input: nums = [1,2,1]
Output: [1,2,1,1,2,1]
Explanation: ans = nums + nums.
Constraints: 1 ≤ n ≤ 1000  |  1 ≤ nums[i] ≤ 1000
Try Examples
Custom:
Approach
Index Mapping — Copy Each Element Twice
Create ans[2n]. For each index i: set ans[i] = nums[i] and ans[i+n] = nums[i]. The result is nums followed by nums again.
Array Visualization
Variables
Index i
nums[i]
ans[i]
ans[i+n]
Step Logic
Press ▶ Play or Next Step to begin.
🎉
Ready
0 / 0
Select an example above and press Play.
Algorithm
1
Get n = nums.length; create ans[2*n]
2
For each i: copy nums[i]ans[i] (first half)
3
Also copy nums[i]ans[i+n] (second half)
4
Return ans — the concatenated array
Time
O(n)
Space
O(n)
Why It Works

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.