

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return the largest array between arrays JavaScript
We have an array of arrays that contains some numbers, we have to write a function that returns the takes in that array and returns the index of the subarray that has the maximum sum. If more than one subarray has the same maximum sum, we have to return the index of first such subarray.
Therefore, let’s write the code for this −
Example
const arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]; const findMaxSubArray = (arr) => { const add = (array) => array.reduce((acc, val) => acc+val); return arr.reduce((acc, val, ind) => { const sum = add(val); if(sum > acc.sum){ return { index: ind, sum } }; return acc; }, { index: -1, sum: -Infinity }).index; }; console.log(findMaxSubArray(arr));
Output
The output in the console will be −
3
- Related Questions & Answers
- Return Largest Numbers in Arrays passed using reduce method?
- Finding the difference between two arrays - JavaScript
- Finding the Largest Triple Product Array in JavaScript
- JavaScript Match between 2 arrays
- Find the Symmetric difference between two arrays - JavaScript
- Picking the largest elements from multidimensional array in JavaScript
- Compare two arrays of single characters and return the difference? JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Constructing largest number from an array in JavaScript
- Difference between the largest and the smallest primes in an array in Java
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- How to find the largest number contained in a JavaScript array?
- Finding the largest non-repeating number in an array in JavaScript
- How to get the difference between two arrays in JavaScript?
- From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
Advertisements