Introduction

The Concatenation of Array problem involves creating a new array by appending the original array to itself.

Given an integer array arr[], the task is to create an array:

 ans = arr + arr

This means:

  • the original array appears twice consecutively

This problem helps in understanding:

  • array traversal
  • indexing techniques
  • array construction
  • sequential copying

Example

Input:arr[] = [1,2,1]
Output:
[1,2,1,1,2,1]
Explanation:
Original array is repeated twice.
Input:
arr[] = [1,3,2,1]
Output:
[1,3,2,1,1,3,2,1]
Explanation:
Second copy is appended after first array.

Constraints

 1 <= n <= 1000 1 <= arr[i] <= 1000

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Create a new array
  2. Traverse original array twice
  3. Insert elements into result array

This approach is easy to understand and directly follows the problem statement.

Steps

  1. Create result array.
  2. Traverse original array first time:
    • insert elements
  3. Traverse original array second time:
    • insert elements again
  4. Return result array.

Dry Run

Input:arr = [1,2,1]

First Traversal:
Add 1
Add 2
Add 1

Result:
[1,2,1]

Second Traversal:
Add 1
Add 2
Add 1

Final Result:
[1,2,1,1,2,1]

Brute Force Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Array is traversed twice.
Space Complexity: O(n)
Explanation:
Extra result array is used.

Approach 2 : Optimized Solution (Direct Index Placement)

Explanation

Instead of traversing twice separately, we can directly place elements into the correct positions using indexing.

The idea is:

  • first half stores original array
  • second half stores copied array

This provides cleaner placement logic.

Steps

  1. Create result array of size 2n.
  2. Traverse original array.
  3. Place:
    • arr[i] at index i
    • arr[i] at index i + n
  4. Return result array.

Dry Run

Input:arr = [1,2,1]

Create result array:
[_,_,_,_,_,_]
Place first 1:
result[0] = 1
result[3] = 1
Result:
[1,_,_,1,_,_]
Place 2:
result[1] = 2
result[4] = 2
Result:
[1,2,_,1,2,_]
Place last 1:
result[2] = 1
result[5] = 1
Final Result:
[1,2,1,1,2,1]

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each element is copied twice.

Space Complexity: O(n)
Explanation:
Extra result array is used.

Edge Cases

  1. Array contains one element
  2. Array contains duplicates
  3. Array contains large values
  4. Array contains all same elements
  5. Empty-like minimal input

Why This Problem is Important

This problem helps in understanding:

  1. Array copying
  2. Sequential traversal
  3. Index placement
  4. Array construction
  5. Memory allocation basics

It is one of the most common beginner-level array interview problems.

Real-World Applications

Array duplication concepts are used in:

  1. Circular buffering
  2. Data replication systems
  3. Multimedia looping
  4. Memory management
  5. Sequential data processing

Common Mistakes

  1. Incorrect result array size
  2. Wrong index placement
  3. Array out-of-bounds errors
  4. Forgetting second copy placement

Interview Tips

Interviewers often expect:

  1. Proper indexing logic
  2. Efficient traversal
  3. Correct array construction

Always explain how the second copy starts from index n.

Related Questions

  1. Shuffle Array
  2. Merge Sorted Arrays
  3. Rotate Array
  4. Duplicate Zeros
  5. Move Zeroes

Final Takeaway

The Concatenation of Array problem is a fundamental array construction problem that teaches indexing and sequential copying techniques. Understanding this problem builds a strong foundation for advanced array manipulation and memory-based interview problems.