Concatenation of Array solution and explanation
9 more videos on this problem
Concatenation of Array - Leetcode 1929 - PythonNeetCodeIO · 4:00 · 113K views
Leetcode | 1929. Concatenation of Array | Easy | Java SolutionDeveloper Docs · 4:05 · 7K views
Concatenation of Array | LeetCode 1929 | C++Knowledge Center · 7:16 · 5K views
1. Leetcode 1929. Concatenation of Array : JAVAWeb Fusion with Bharadwaj · 11:45 · 1K views
1929. Concatenation of Array (Leetcode Easy)Programming Live with Larry · 2:20 · 1K views
1929 Concatenation of Array - LeetCode JavascriptBecoming A Better Dev (with David) · 3:50 · 745 views
Concatenation Of Array | Leetcode 1929 | javaPiyush Raj · 5:25 · 728 views
1929. Concatenation of Array Solution - Leetcode - Python 3Archive · 1:50 · 668 views
1929. Concatenation of Array | LeetCode | JavaAthiyaStudies · 11:33 · 652 views
1929. Leetcode Concatenation of Array - EasySmile And Code · 6:54 · 562 views
The problem
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] <= 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.
def get_concatenated_array(nums):
return nums + nums
nums = [1, 2, 1]
ans = get_concatenated_array(nums)
print(ans)import java.util.Arrays;
public class Concatenation {
public static int[] getConcatenatedArray(int[] nums) {
int n = nums.length;
int[] ans = new int[2 * n];
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
public static void main(String[] args) {
int[] nums = {1, 2, 1};
int[] ans = getConcatenatedArray(nums);
System.out.println(Arrays.toString(ans));
}
}#include <vector>
#include <iostream>
std::vector<int> getConcatenatedArray(std::vector<int>& nums) {
std::vector<int> ans;
ans.reserve(2 * nums.size());
ans.insert(ans.end(), nums.begin(), nums.end());
ans.insert(ans.end(), nums.begin(), nums.end());
return ans;
}
int main() {
std::vector<int> nums = {1, 2, 1};
std::vector<int> ans = getConcatenatedArray(nums);
for (int num : ans) {
std::cout << num << " ";
}
return 0;
}function getConcatenatedArray(nums) {
return nums.concat(nums);
}
const nums = [1, 2, 1];
const ans = getConcatenatedArray(nums);
console.log(ans);#include <stdio.h>
#include <stdlib.h>
int* getConcatenatedArray(int* nums, int numsSize, int* returnSize) {
*returnSize = 2 * numsSize;
int* ans = (int*)malloc((*returnSize) * sizeof(int));
for (int i = 0; i < numsSize; ++i) {
ans[i] = nums[i];
ans[i + numsSize] = nums[i];
}
return ans;
}
int main() {
int nums[] = {1, 2, 1};
int returnSize;
int* ans = getConcatenatedArray(nums, 3, &returnSize);
for (int i = 0; i < returnSize; ++i) {
printf("%d ", ans[i]);
}
free(ans);
return 0;
}using System;
public class Solution {
public static int[] GetConcatenatedArray(int[] nums) {
int n = nums.Length;
int[] ans = new int[2 * n];
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
public static void Main(string[] args) {
int[] nums = {1, 2, 1};
int[] ans = GetConcatenatedArray(nums);
Console.WriteLine(string.Join(", ", ans));
}
}In Python, you can simply use the `+` operator to concatenate lists, which is a very efficient and clean solution to achieve the required duplication.
Time Complexity: O(n), where n is the number of elements in `nums`.
Space Complexity: O(n), due to the creation of the `ans` list.
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.
def get_concatenated_array(nums):
return nums * 2
nums = [1, 2, 1]
ans = get_concatenated_array(nums)
print(ans)import java.util.ArrayList;
import java.util.List;
public class Concatenation {
public static List<Integer> getConcatenatedArray(List<Integer> nums) {
List<Integer> ans = new ArrayList<>(nums);
ans.addAll(nums);
return ans;
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(List.of(1, 2, 1));
List<Integer> ans = getConcatenatedArray(nums);
System.out.println(ans);
}
}#include <vector>
#include <iostream>
std::vector<int> getConcatenatedArray(std::vector<int>& nums) {
std::vector<int> ans;
ans.reserve(2 * nums.size());
ans.insert(ans.end(), nums.begin(), nums.end());
ans.insert(ans.end(), nums.begin(), nums.end());
return ans;
}
int main() {
std::vector<int> nums = {1, 2, 1};
std::vector<int> ans = getConcatenatedArray(nums);
for (int num : ans) {
std::cout << num << " ";
}
return 0;
}function getConcatenatedArray(nums) {
return nums.concat(nums);
}
const nums = [1, 2, 1];
const ans = getConcatenatedArray(nums);
console.log(ans);/* Though C typically uses fixed arrays, the following approach is similar to the dynamic behavior of a list. */
#include <stdio.h>
#include <stdlib.h>
int* getConcatenatedArray(int* nums, int numsSize, int* returnSize) {
*returnSize = 2 * numsSize;
int* ans = (int*)malloc((*returnSize) * sizeof(int));
for (int i = 0; i < numsSize; ++i) {
ans[i] = nums[i];
ans[i + numsSize] = nums[i];
}
return ans;
}
int main() {
int nums[] = {1, 2, 1};
int returnSize;
int* ans = getConcatenatedArray(nums, 3, &returnSize);
for (int i = 0; i < returnSize; ++i) {
printf("%d ", ans[i]);
}
free(ans);
return 0;
}using System;
using System.Collections.Generic;
public class Concatenation {
public static List<int> GetConcatenatedArray(List<int> nums) {
List<int> ans = new List<int>(nums);
ans.AddRange(nums);
return ans;
}
public static void Main(string[] args) {
List<int> nums = new List<int> { 1, 2, 1 };
List<int> ans = GetConcatenatedArray(nums);
Console.WriteLine(string.Join(", ", ans));
}
}Python allows duplication using the `*` operator, here used to repeat `nums` twice directly.
Time Complexity: O(n)
Space Complexity: O(n)
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.
function getConcatenation(nums: number[]): number[] {
return [...nums, ...nums];
}func getConcatenation(nums []int) []int {
return append(nums, nums...)
}impl Solution {
pub fn get_concatenation(nums: Vec<i32>) -> Vec<i32> {
nums.repeat(2)
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Using a Direct Copy Approach | Time Complexity: O(n), where n is the number of elements in `nums`. |
| Using Array List or Dynamic List Approach | Time Complexity: O(n) |
| Simulation | – |
Complexity analysis
| 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 |
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.
