LeetCode Challenges #2: Concatenation of Array
Exploring ‘easy’ LeetCode problems: solutions, strategies, and insights. Dive into my journey with coding challenges!
Background
Continuing my journey through LeetCode challenges, I encountered yet another intriguing problem. This time, the task was to concatenate an array with itself, a seemingly simple task with interesting implications. Join me as I unravel my approach to solving this problem and the insights gained along the way.
Problem: Concatenation of Array
The question is like: Given an integer array nums of length n, the objective is to create an array ans of length 2n such that ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Essentially, ans is the concatenation of two nums arrays.
Understanding the Problem
The problem statement is quite clear: we need to extend the given array by appending its own elements to it. The catch lies in how we efficiently achieve this task, ensuring the resulting array satisfies the specified conditions. It’s essential to devise a strategy that not only works but also performs optimally.
Solving the Problem
To tackle this problem, I opted for a simple and efficient approach using PHP. Here’s how I implemented the solution:
<?php
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function getConcatenation($nums) {
$inputLength = count($nums);
$outputLength = 2 * $inputLength;
for ($i = 1; $i <= $outputLength; $i++) {
if ($i > $inputLength) {
array_push($nums, $nums[($i - $inputLength) - 1]);
}
}
return $nums;
}
}
?>
In this solution:
- We calculate the length of the input array nums and store it in $inputLength.
- We calculate the desired length of the output array, which is twice the length of the input array, and store it in $outputLength.
- We then iterate over each index from 1 to $outputLength.
- For indices beyond the length of the input array, we append elements from the input array to ensure that ans[i + n] == nums[i].
- Finally, we return the concatenated array.
Optimization and Performance
Upon submission, the solution performed well with an optimized runtime. It successfully concatenated the array while maintaining the specified conditions.

More optimized result…
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function getConcatenation($nums) {
$inputLength = count($nums);
$output = array_fill(0, 2 * $inputLength, 0);
for ($i = 0; $i < $inputLength; $i++) {
$output[$i] = $nums[$i];
$output[$i + $inputLength] = $nums[$i];
}
return $output;
}
}
Approach Justification
- This approach is chosen because it offers a more straightforward and efficient solution compared to the previous approach.
- Instead of manipulating the input array within a loop, we directly construct the concatenated array, avoiding unnecessary array manipulation operations.
- By preallocating the output array and directly copying elements into their correct positions, we achieve better performance and maintain readability.

If you like to read further …
This problem provided an opportunity to delve deeper into array manipulation and problem-solving strategies. By applying a straightforward approach and optimizing where possible, I was able to effectively concatenate the array, meeting the requirements set forth by the problem statement.
With each challenge tackled, I gain valuable insights and refine my problem-solving skills. Join me on this journey as we continue to explore the fascinating world of LeetCode challenges.
Happy coding! 🚀
Thank you for reading!
Also…
This blog post provides insights into my approach to solving the problem of concatenating an array with itself. Through efficient coding and problem-solving strategies, I successfully tackled the challenge while optimizing performance.
I hope you find this post insightful and informative. Stay tuned for more exciting LeetCode challenges and solutions!
If you have any questions or suggestions, feel free to leave a comment below. Let’s learn and grow together in our coding journey.
Until next time, happy coding!