dcheepurapalli
dcheepurapalli

Reputation: 75

Find the array which is small in length among other arrays efficiently

How can i find which is the smallest array efficiently in javascript

var ar1 = [1, 5, 10, 20, 40, 80];
var ar2 = [6, 7, 20, 80, 100];
var ar3 = [3, 4, 15, 20, 30, 70, 80, 120];

I am not interested in using multiple if condition. Suggest me efficient way to find the min sized array in javascript.

if(ar1.length < ar2.length){
  if(ar1.length < ar3.length){
    console.log("ar1");
   }else{
    console.log("ar3");
   }
}else{
   if(ar2.length < ar3.length){
     console.log("ar2");
   }else{
    console.log("ar3");
  }
}

Thanks

Upvotes: 1

Views: 82

Answers (7)

user2490157
user2490157

Reputation:

Using recursive ternary operators, you can easily get the minimum length and then assign it to the array:

var mlen = Math.min(ar1.length,ar2.length,ar3.length);
var str = (ar1.length===mlen ? "ar1":(ar2.length===mlen ? "ar2":"ar3"));

If you really want to log it:

console.log(str);

Full Code

var ar1 = [1, 5, 10, 20, 40, 80];
var ar2 = [6, 7, 20, 80, 100];
var ar3 = [3, 4, 15, 20, 30, 70, 80, 120];

var mlen = Math.min(ar1.length,ar2.length,ar3.length);
var str = (ar1.length===mlen ? "ar1":(ar2.length===mlen ? "ar2":"ar3"));

document.getElementById("plh").innerHTML = str;
<div id="plh"></div>

Upvotes: 0

Marcus Henrique
Marcus Henrique

Reputation: 782

You can do this in a more functional way too:

var ar1 = [1, 5, 10, 20, 40, 80];
var ar2 = [6, 7, 20, 80, 100];
var ar3 = [3, 4, 15, 20, 30, 70, 80, 120];

var smallest = [ar1, ar2, ar3].reduce(function (previous, current) {
    if (previous.length === 0) {
        return current;
    }
    return current.length < previous.length ? current : previous;
}, []);

console.log(smallest);

Upvotes: 0

kavetiraviteja
kavetiraviteja

Reputation: 2208

var ar1 = [1, 5, 10, 20, 40, 80];
var ar2 = [6, 7, 20, 80, 100];
var ar3 = [3, 4, 15, 20, 30, 70, 80, 120];
var smallest = ar1;
if (smallest.length > ar2.length) smallest = ar2;
if (smallest.length > ar3.length) smallest = ar3;

Upvotes: 0

epascarello
epascarello

Reputation: 207501

Create a method that accepts all your arrays, loop and compare the lengths.

var ar1 = [1, 5, 10, 20, 40, 80];
var ar2 = [6, 7, 20, 80, 100];
var ar3 = [3, 4, 15, 20, 30, 70, 80, 120];

function returnSmallest() {

  var smallest = arguments[0];
  for(var i=1;i<arguments.length;i++){
      if(smallest.length>arguments[i].length) {
          smallest = arguments[i];  
      }
  }
  return smallest;
}

console.log(returnSmallest(ar1,ar2,ar3));

Upvotes: 1

Thibault Sottiaux
Thibault Sottiaux

Reputation: 197

You can put the array lengths into an other array

var arrayLengths = [arr1.length, arr2.length, arr3.length];

and then use the built-in functions of the Math library in order to find which array is the largest/smallest

var arrI = arrayLengths.indexOf(Math.max.apply(Math, arrayLengths));

Note that if two arrays are of the minimum length, this will give you the index of the first one.

Upvotes: 0

Try:

var ar1 = [1, 5, 10, 20, 40, 80];
var ar2 = [6, 7, 20, 80, 100];
var ar3 = [3, 4, 15, 20, 30, 70, 80, 120];

function compareSmall(array1, array2) {
    if (array1.length < array2.length) 
        return array1;
    else
        return array2;
}

console.log(compareSmall(compareSmall(ar1,ar2),ar3));

Upvotes: 0

Oriol
Oriol

Reputation: 288000

You can create an object which associates each array with its name (also consider using an array).

Then just iterate it, and compare lengths.

var obj = {ar1: ar1, ar2: ar2, ar3: ar3 /*...*/},
    minLen = Infinity,
    minName;
for(var name in obj)              /* Iterate the arrays */
  if(obj[name].length < minLen) { /* Smaller array found */
    minLen = obj[name].length;    /* Update the minimum length */
    minName = name;               /* Store its name */
  }
console.log(minName);

Upvotes: 1

Related Questions