Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get the smallest array from an array of arrays in JavaScript
When working with nested arrays in JavaScript, you might need to find the smallest subarray based on the number of elements. This is useful for data processing, filtering operations, or when you need to identify the shortest path or sequence.
Suppose we have a nested array of arrays like this:
const arr = [
["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],
["RIGHT", "LEFT", "TOP"],
["TOP", "LEFT"]
];
console.log("Original array:", arr);
Original array: [ [ 'LEFT', 'RIGHT', 'RIGHT', 'BOTTOM', 'TOP' ], [ 'RIGHT', 'LEFT', 'TOP' ], [ 'TOP', 'LEFT' ] ]
We need a JavaScript function that takes such an array and returns the smallest subarray (smallest in terms of number of elements). If multiple subarrays have the same smallest length, the function returns all of them.
Using Array.reduce() Method
The most efficient approach uses the reduce() method to iterate through the array once and track the smallest subarrays:
const arr = [
["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],
["RIGHT", "LEFT", "TOP"],
["TOP", "LEFT"]
];
const findShortest = (arr = []) => {
const res = arr.reduce((acc, val, ind) => {
if (!ind || val.length
[ [ 'TOP', 'LEFT' ] ]
How It Works
The function works by:
- Initialization: The accumulator starts as an empty array
-
First element: When
indis 0, it sets the first subarray as the current shortest - Comparison: For subsequent elements, it compares lengths with the current shortest
- Update: If a shorter array is found, it replaces the accumulator; if equal length, it adds to the accumulator
Alternative Method: Using Math.min()
Here's another approach that first finds the minimum length, then filters arrays with that length:
const findShortestAlt = (arr = []) => {
const minLength = Math.min(...arr.map(subArr => subArr.length));
return arr.filter(subArr => subArr.length === minLength);
};
const arr = [
["LEFT", "RIGHT", "RIGHT", "BOTTOM", "TOP"],
["RIGHT", "LEFT", "TOP"],
["TOP", "LEFT"],
["UP", "DOWN"] // Another array with same minimum length
];
console.log(findShortestAlt(arr));
[ [ 'TOP', 'LEFT' ], [ 'UP', 'DOWN' ] ]
Handling Edge Cases
The function handles empty arrays and returns an empty array when no subarrays exist:
// Empty array console.log(findShortest([])); // Array with empty subarrays const arrWithEmpty = [[], ["A"], ["B", "C"]]; console.log(findShortest(arrWithEmpty));
[] [ [] ]
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
reduce() |
O(n) | O(k) | Moderate |
Math.min() + filter() |
O(2n) | O(k) | High |
Where n is the number of subarrays and k is the number of shortest subarrays.
Conclusion
The reduce() method provides the most efficient single-pass solution for finding the smallest subarrays. The alternative Math.min() approach offers better readability but requires two passes through the data.
