DSA AnimatorDSA animations
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.

How to solve Concatenation of Array

LeetCode 1929 · Easy · Arrays · Pattern: Basics

Why the obvious approach is slow

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.

The key idea

Every value at index i is copied to two places. Once at index i, and once at index i plus n.

Remember

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.

More Arrays problems