class Solution:
def getConcatenation(self, nums):
# Concatenate the list with itself using the + operator
return nums + nums
class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
vector<int> ans(nums);
ans.insert(ans.end(), nums.begin(), nums.end());
return ans;
}
};
class Solution {
public int[] getConcatenation(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;
}
}
/**
* @param {number[]} nums
* @return {number[]}
*/
var getConcatenation = function(nums) {
return nums.concat(nums);
};
Given an integer array nums
, your task is to return a new array ans
such that ans
is the concatenation of nums
with itself. In other words, if nums = [a, b, c]
, then ans = [a, b, c, a, b, c]
.
nums
must appear twice and in the same order.
The problem asks us to create a new array by joining the input array nums
with itself. Initially, you might think about using a loop to add each element two times, or perhaps creating a new array and copying elements one by one. However, most programming languages have built-in methods to join or concatenate arrays efficiently.
The brute-force approach would involve iterating through nums
and appending each element twice to the result. But since we want an array that is exactly nums
followed by nums
again, we can simply concatenate the array with itself.
By leveraging built-in functions or efficient array operations, we can write a concise and efficient solution.
Let's break down the steps to solve this problem:
nums
followed by all elements of nums
again, in the same order.nums + nums
concatenates two lists.nums.concat(nums)
joins two arrays.insert
to append elements from one vector to another.nums
.This approach is very efficient because array concatenation is optimized in most languages. We avoid unnecessary loops or manual copying where possible, except in languages like Java or C++ where array manipulation requires explicit handling.
Let's work through an example. Suppose nums = [1, 2, 3]
.
nums = [1, 2, 3]
.nums
with itself: [1, 2, 3] + [1, 2, 3]
.[1, 2, 3, 1, 2, 3]
.Each step simply takes the entire array and appends it to itself, preserving order and duplicating every element exactly once.
nums
and append each element twice, you still process 2n
elements, so the time complexity is O(n)
. The space complexity is also O(n)
for the result array of size 2n
.n
results in a new array of length 2n
. The time complexity is O(n)
for copying elements, and the space complexity is O(n)
for the new array.In both cases, the operation is linear with respect to the size of the input array.
This problem demonstrates how understanding built-in array operations can lead to concise and efficient solutions. By recognizing that concatenation is all that's needed, we avoid unnecessary complexity and make the code readable. The key insight is to use the language's native array joining capabilities, resulting in an elegant O(n)
time and space solution.