Skip to main content

Command Palette

Search for a command to run...

Leetcode 1929. Concatenation of Array

Updated
1 min read
K

I am a developer from Nashville, TN. I specialize in the .NET tech stack. I have created many projects in Blazor WASM, Xamarin, MAUI, etc.

Intuition

Loop through input array and fill answer array in one pass.

Approach

In one forloop set the ans[i] and ans[i + nums.Length]
to the current value at nums[i].

Code

public class Solution 
{
    public int[] GetConcatenation(int[] nums) 
    {
        var numsLength = nums.Length;
        var ansLength = numsLength * 2;
        int[] ans = new int[ansLength];

        for(int i = 0; i < numsLength; i++)
        {
           ans[i] = nums[i];
           ans[i + numsLength] = nums[i];
        }

        return ans;
    }
}

LeetCode journey

Part 7 of 9

A series documenting my journey to improving my ability to solve LeetCode problems through YouTube videos, study plans, articles, etc. Using my own words for later reference.

Up next

Leetcode 347. Top K Frequent Elements

Shout out to NeetCode for this solution

More from this blog

Untitled Publication

10 posts