Skip to main content

Concatenation of Array - Solution & Explanation

EasyArraySimulation11 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

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.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000

Approach Overview

Problem 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 1: Using a Direct Copy Approach

This approach involves creating a new array of length 2n and filling it by manually copying the original array twice.

We allocate memory for an array 'ans' of size 2 * `numsSize` and use a loop to copy elements from `nums` into `ans` for each index. After filling ans, which is manually managed in C, use `free` to deal with memory allocation.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of elements in `nums`.
Space Complexity: O(n), due to the creation of the `ans` array.

Try this approach in the editor β†’

Approach 2: Using Array List or Dynamic List Approach

Here, lists such as ArrayLists in Java or similar dynamic structures in other languages can be used to append the `nums` array to itself.

In C, dynamic lists are not natively supported, but we simulate the behavior by directly allocating an array of appropriate size.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n)

Try this approach in the editor β†’

Approach 3: Simulation

We directly simulate according to the problem description by adding the elements of nums to the answer array one by one, and then adding the elements of nums to the answer array again.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
Using a Direct Copy Approach

Time Complexity: O(n), where n is the number of elements in `nums`.
Space Complexity: O(n), due to the creation of the `ans` array.

Using Array List or Dynamic List Approach

Time Complexity: O(n)
Space Complexity: O(n)

Simulationβ€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Copy ApproachO(n)O(n)Best general solution when final array size (2n) is known ahead of time
Array List / Dynamic ListO(n)O(n)Useful when working with dynamic containers like Python lists or Java ArrayList

Video Solution

Concatenation of Array - Leetcode 1929 - Python β€’ NeetCodeIO β€’ 113,705 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Concatenation of Array easy or hard?
Concatenation of Array is categorized as an Easy problem on LeetCode with a very high acceptance rate. It focuses on fundamental array operations and is often used as a beginner exercise for iteration and indexing.
Concatenation of Array Python/Java solution
In Python, you typically create a list of size 2 * n and assign values using indices or simply use list concatenation like nums + nums. In Java, allocate an int array of size 2 * nums.length and copy elements using a loop.
How to solve Concatenation of Array in O(n)?
Create a new array of length 2n and iterate through nums once. For every index i, assign ans[i] = nums[i] and ans[i + n] = nums[i]. This builds both halves of the result during the same pass, giving linear time complexity.
What is the best approach for Concatenation of Array?
The direct copy approach is the best solution. Allocate a result array of size 2n and copy each element from nums into positions i and i + n during a single loop. This runs in O(n) time and uses O(n) extra space, which is optimal for this problem.
Is Concatenation of Array asked at Google/Amazon/Meta?
Concatenation-style array construction problems appear frequently in coding screens at companies like Amazon and Meta as warm‑up questions. They test basic array manipulation, indexing, and understanding of time and space complexity.
What data structure is used in Concatenation of Array?
The primary data structure is an array. Some implementations also use dynamic lists such as Python lists, Java ArrayList, or C++ vectors to build the result before returning it.
What is the time complexity of Concatenation of Array?
The optimal solution runs in O(n) time where n is the length of the input array. Each element is processed once and copied into two positions in the result array. Space complexity is O(n) because the output array contains 2n elements.

Ready to solve this problem?

Practice Concatenation of Array with our built-in code editor and test cases.

Practice on FleetCode