Unzipping Arrays in JavaScript
Unzipping arrays, also known as transposing arrays, involves taking an array of arrays and converting it into a single array where each element is an array containing the corresponding elements from the original arrays. This is a common operation in data processing and can be useful for transforming data structures for analysis or visualization. This challenge will test your ability to manipulate arrays and understand how to iterate through them effectively.
Problem Description
You are given an array of arrays (a 2D array) where each inner array has the same length. Your task is to write a JavaScript function called unzip that takes this 2D array as input and returns a new array of arrays representing the "unzipped" version. The resulting array should have the same number of inner arrays as the original array has elements. Each inner array in the result should contain the elements at the corresponding index from each of the original inner arrays.
Key Requirements:
- The function must accept a 2D array as input.
- The function must return a new 2D array (not modify the original).
- All inner arrays in the input array must have the same length.
- The function should handle empty input arrays gracefully.
- The function should handle cases where the inner arrays are empty.
Expected Behavior:
The unzip function should iterate through the input array and create a new array of arrays. For each index, it should collect the elements from the corresponding index of each inner array and place them into a new inner array.
Edge Cases to Consider:
- Empty input array: Should return an empty array.
- Inner arrays with different lengths: Should throw an error (see Constraints).
- Empty inner arrays: Should still function correctly, potentially resulting in empty inner arrays in the output.
Examples
Example 1:
Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Explanation: The function takes the first element from each inner array (1, 4, 7) to form the first inner array of the output. It then takes the second element from each inner array (2, 5, 8) to form the second inner array, and so on.
Example 2:
Input: [[1, 2], [3, 4], [5, 6]]
Output: [[1, 3, 5], [2, 4, 6]]
Explanation: Similar to Example 1, but with shorter inner arrays.
Example 3:
Input: []
Output: []
Explanation: An empty input array should result in an empty output array.
Example 4:
Input: [[], [], []]
Output: [[], [], []]
Explanation: Input with empty inner arrays.
Constraints
- The input array will contain only numbers.
- All inner arrays within the input array must have the same length. If they do not, the function should throw a
TypeErrorwith the message "Inner arrays must have the same length." - The function should have a time complexity of O(m*n), where 'm' is the number of inner arrays and 'n' is the length of each inner array.
- The function should have a space complexity of O(m*n) to store the unzipped array.
Notes
Consider using nested loops to iterate through the input array. Think about how to build the new array of arrays incrementally. Error handling is important to ensure the function behaves predictably with invalid input. The key is to access elements at the same index across all inner arrays to construct the unzipped result.