Flatten Nested Array

Medium~15 min

Given a 2D array (an array of arrays of numbers), flatten it into a single 1D array.

The input is only one level deep — each element of the outer array is itself an array of numbers. Concatenate all the inner arrays in order to produce the result.

Examples

Example 1
Input: arr = [[1, 2], [3, 4], [5, 6]]
Output: [1, 2, 3, 4, 5, 6]
Explanation: The three inner arrays are concatenated in order.
Example 2
Input: arr = [[1], [2, 3, 4], [5]]
Output: [1, 2, 3, 4, 5]
Explanation: Inner arrays can be different lengths.
Example 3
Input: arr = [[], [1, 2], []]
Output: [1, 2]
Explanation: Empty inner arrays contribute nothing to the result.

Constraints

  • 0 <= arr.length <= 10^3
  • 0 <= arr[i].length <= 10^3
  • -10^6 <= arr[i][j] <= 10^6
  • Total elements across all inner arrays <= 10^5
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run