Implement Array.concat

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Array.concat

Approach

Create a new result array, copy all elements from the original array, then iterate through each argument. If an argument is an array, spread its elements individually into the result; if it is a scalar value, push it directly. This produces a new array without mutating the originals.

Complexity Analysis

Time
O(n + m)
Space
O(n + m)

Pattern

Array Polyfill

Why It Works

Concat produces a shallow copy that merges arrays and values into a single flat array. It only flattens one level of array arguments, leaving nested arrays intact.

Updated Feb 2026