1

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
  }
6
  • 3
    You could flatten, sort by length, pick the first. Commented Jul 10, 2016 at 19:15
  • I want to find the biggest sub array not word length! Commented Jul 10, 2016 at 19:20
  • you have to do it with recursion Commented Jul 10, 2016 at 19:21
  • 1
    @webdeb Why does the recursion come into here? Commented Jul 10, 2016 at 19:35
  • 1
    @PraveenKumar Please check my answer to see why I would choose a recursive approach, in my opinion its cleaner Commented Jul 10, 2016 at 19:41

2 Answers 2

2

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..]]
Sign up to request clarification or add additional context in comments.

2 Comments

But seriously.. I didn't think of this way, provided that the original array is in a static form and giving a simpler solution for a newbie programmer was in my mind. Do you agree? :)
@PraveenKumar When I create a function, I expect to reuse it. To solve such a simple task, a one-liner is more applicable, see edits
1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.