Zip Two Lists

Easy~10 min

Given two arrays of integers a and b, return a new array of pairs (as 2-element arrays) where each pair contains one element from each input array at the same index.

If the arrays have different lengths, stop at the shorter one. Only pair up elements at indices that exist in both arrays.

Examples

Example 1
Input: a = [1, 2, 3], b = [4, 5, 6]
Output: [[1, 4], [2, 5], [3, 6]]
Explanation: Each element at index i from a is paired with the element at index i from b.
Example 2
Input: a = [1, 2], b = [3, 4, 5]
Output: [[1, 3], [2, 4]]
Explanation: b has an extra element at index 2, but a only has 2 elements, so we stop after 2 pairs.
Example 3
Input: a = [10], b = [20]
Output: [[10, 20]]
Explanation: Single-element arrays produce a single pair.

Constraints

  • 0 <= a.length, b.length <= 10^4
  • -10^9 <= a[i], b[i] <= 10^9
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run