- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Return Largest Numbers in Arrays passed using reduce method?
- Finding the Largest Triple Product Array in JavaScript
- Finding the difference between two arrays - JavaScript
- JavaScript Match between 2 arrays
- Compare two arrays of single characters and return the difference? JavaScript
- Picking the largest elements from multidimensional array 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
- Get the smallest array from an array of arrays in JavaScript
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Find the Symmetric difference between two arrays - JavaScript
- Array of objects to array of arrays in JavaScript
- Constructing largest number from an array in JavaScript
- Partial sum in array of arrays JavaScript
- How to find the largest number contained in a JavaScript array?
- Finding the largest non-repeating number in an array in JavaScript

Advertisements