#1929

Concatenation of Array

Easy Array basics

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return an array of length 2n that is the input array followed by itself.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: make an array twice as long that holds the input two times in a row.

Why this works: Java arrays have a fixed size, so you create the full 2n array first and fill both halves with simple index maths.

Example: nums = [1, 2, 1]

ans has length 6i=0: ans[0]=1, ans[3]=1i=1: ans[1]=2, ans[4]=2i=2: ans[2]=1, ans[5]=1ans = [1, 2, 1, 1, 2, 1]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Trying to "append" to a Java int[] — arrays can't grow. Allocate the full size first.