Watch 10 video solutions for Concatenation of Array, a easy level problem involving Array, Simulation. This walkthrough by NeetCodeIO has 87,021 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: nums = [1,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] - ans = [1,2,1,1,2,1]
Example 2:
Input: nums = [1,3,2,1] Output: [1,3,2,1,1,3,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]] - ans = [1,3,2,1,1,3,2,1]
Constraints:
n == nums.length1 <= n <= 10001 <= nums[i] <= 1000Problem Overview: You receive an integer array nums of length n. The task is to construct a new array ans of length 2n where the first half is nums and the second half is again nums. In other words, ans[i] = nums[i] and ans[i + n] = nums[i] for every index 0 ≤ i < n. The result is simply the original array concatenated with itself.
Approach 1: Direct Copy Approach (O(n) time, O(n) space)
Create a result array of size 2 * n. Iterate through the original array once and copy each value into two positions: the current index and the index shifted by n. Specifically, during iteration set ans[i] = nums[i] and ans[i + n] = nums[i]. This works because the first half and second half of the result are identical. The algorithm performs a single pass over the input array, making it linear time with straightforward memory usage.
This method is the most direct implementation and works in every language with basic array operations. It avoids resizing or repeated allocations because the final array size is known ahead of time. Most interview solutions use this exact structure because it clearly demonstrates control over indexing.
Approach 2: Array List or Dynamic List Approach (O(n) time, O(n) space)
Instead of allocating the final array immediately, use a dynamic structure such as an ArrayList, vector, or Python list. Iterate through nums once and append each element to the list. Then iterate again and append the same elements a second time. Dynamic containers automatically grow as elements are added, so you don't need to manage capacity manually.
This approach is common in languages where dynamic lists are the default structure. The total number of append operations is 2n, so the time complexity remains O(n). Space complexity is also O(n) because the result holds twice the original elements. The logic relies on simple iteration and falls under basic simulation patterns where the output is constructed step by step.
Recommended for interviews: The direct copy approach is typically preferred. Interviewers expect you to recognize that the final array size is known and fill both halves during a single pass. It demonstrates clear reasoning about indices and efficient use of arrays. The dynamic list approach is also acceptable, but the direct indexing version shows stronger control over memory layout and is usually the cleanest implementation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Copy Approach | O(n) | O(n) | Best general solution when final array size (2n) is known ahead of time |
| Array List / Dynamic List | O(n) | O(n) | Useful when working with dynamic containers like Python lists or Java ArrayList |