dijam
dijam

Reputation: 678

Find biggest subarray in a 2d array in JavaScript

Given an array I want to find the largest sub array by the length i.e

var table = [
               ["Protein",["Grilled Chicken","Lamb"]],
               ["Fats",["rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"]],
               ["Vegatables",["Carrots","Eggs","Milks","Peppers"]]
              ];

I want it to return ["Carrots","Eggs","Milks","Peppers"]

Heres my code

function findBiggestSubArray(array){
    var biggestArrayIndex = 0;

    for(var i=0;i<array.length;i++){
      if(i === (array.length-1)){
        //We have reached the end of the array then return the array
        console.log("Reached the End");
        return array[biggestArrayIndex];
      } else {
        if(!array[biggestArrayIndex][1].length >= array[i][1].length){
          biggestArrayIndex = i;
        }//End of Inner else block
      }//End of Outer else block
    }//End of forloop
  }

Upvotes: 1

Views: 214

Answers (2)

webdeb
webdeb

Reputation: 13211

General solution, to find the most largest array in an array-structure:

I would do it with recursion, so the most biggest Array will be found, in any depth..

/**
 *  array -> The array to check,  
 *  biggestArray -> The most biggestArray found so far
 */
function findBiggestArray(array, biggestArray){
  biggestArray = biggestArray || [];

  if (array.length > biggestArray.length)
    biggestArray = array;

  for (var i = 0; i < array.length; i++) {
    if (array[i] instanceof Array)
      biggestArray = findBiggestArray(array[i],biggestArray)
  }

  return biggestArray;
}

var multiArray = [
  ["1", "2", ["234", "334"]],
  [1,2,3,4,5, [1,2,3,4,5,6,7,7]]
]

var biggest = findBiggestArray(multiArray)
console.log(biggest) 

// This also works!
console.log(findBiggestArray([1, [1,2,3]]))

Oneliner for this special case

// Sort the array by the length of the subarray at position 1, and return the first item
var category = table.sort(function(a, b) { return b[1].length - a[1].length })[0]
// ES6-Syntax
var category = table.sort((a, b) => b[1].length - a[1].length)[0]

category // => ["CategoryName", [ITEMS..]]

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167240

I would do this way (see the comments in the code for explanation):

var table = [
  ["Protein", ["Grilled Chicken", "Lamb"]],
  ["Fats", ["rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"]],
  ["Vegatables", ["Carrots", "Eggs", "Milks", "Peppers"]]
];
function findBiggestSubArray (array) {
  // Initialise empty array.
  var bigSubArray = ["", []];
  // Loop through the given array.
  for (var i = 0; i < array.length; i++) {
    // Check if the current biggest one is bigger than the saved array.
    if (array[i][1].length > bigSubArray[1].length) {
      // If bigger, replace it with current array.
      bigSubArray = array[i];
    }
  }
  // Return the biggest sub array.
  return bigSubArray[1];
}
console.log(findBiggestSubArray(table));

Upvotes: 1

Related Questions