Concatenation Of Array
Duplicate array content in sequence to create a 2n output.
What This Problem Is Asking
In one sentence
Return an array containing the original values followed by the same values again.
Full problem statement
Concatenation of Array
Given an integer array nums of length n, return an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for all 0 <= i < n (0-indexed).
The output is the original array followed by the same elements again in the same order.
Recognition Clues
Signals that this pattern likely applies.
- Output length is a direct multiple of input length.
- Result format is explicit and deterministic.
- A single pass append loop is sufficient.
Why This Pattern Fits
Direct array duplication keeps implementation predictable and linear.
Daily usefulness: Useful for cyclic UI lists and repeated fixture generation.
Interview usefulness: Reinforces array construction and complexity reasoning fundamentals.
Real-world relevance: Common in carousels, playback queues, and looped UI experiences.
Code Snippets
1class Solution {
2 func getConcatenation(_ nums: [Int]) -> [Int] {
3 var result: [Int] = []
4 result.reserveCapacity(nums.count * 2)
5
6 for num in nums {
7 result.append(num)
8 }
9
10 for num in nums {
11 result.append(num)
12 }
13
14 return result
15 }
16}Interactive Carousel Demo
You start with three slides from your list. Press Add next 3 (duplicate) to append the same three values again—the same idea as nums + nums for an infinite carousel track.
[1,2,1,1,2,1]Practical Use Cases
- Build infinite carousel source data in UI layers.
- Repeat seed data for integration demo scenarios.
- Prepare looping playlist buffers.
Demo Ideas
Interactive ways to teach this algorithm.
- Animate first and second append passes separately.
- Show output index mapping against input index.