Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1929. Concatenation of Array - Leetcode Solution

Code Implementation

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);
};
      

Problem Description

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].

  • Each element from nums must appear twice and in the same order.
  • You cannot reuse elements in a way that changes their order or duplicates them more than twice.
  • There is only one valid solution for any given input array.

Thought Process

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.

Solution Approach

Let's break down the steps to solve this problem:

  1. Understand the Goal: We need to create a new array that contains all elements of nums followed by all elements of nums again, in the same order.
  2. Choose the Right Operation: Most languages provide an array concatenation operation. For example:
    • In Python, nums + nums concatenates two lists.
    • In JavaScript, nums.concat(nums) joins two arrays.
    • In Java, we need to create a new array of double the length and copy elements manually.
    • In C++, we can use insert to append elements from one vector to another.
  3. Return the Result: The result should be a new array, not modifying the original 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.

Example Walkthrough

Let's work through an example. Suppose nums = [1, 2, 3].

  1. Start with nums = [1, 2, 3].
  2. Concatenate nums with itself: [1, 2, 3] + [1, 2, 3].
  3. The result is [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.

Time and Space Complexity

  • Brute-force Approach: If you loop through 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.
  • Optimized Approach (Concatenation): Concatenating two arrays of length 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.

Summary

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.