First common element across multiple arrays in Javascript -


i need find first common element across group of arrays. number of arrays may vary, in sequential order (small->large). arrays properties of myobj.

this have far:

function compare(myobj,v,j) {   if (myobj[j].indexof(v)>-1) return true;   else return false; }  function leastcommon ([1,5]) {   var myobj = { //this filled code, finished result looks     1: [1, 2,...,60,...10k]     2: [2, 4,...,60,...20k]     3: [3, 6,...,60,...30k]     4: [4, 8,...,60,...40k]     5: [5,10,...,60,...50k]   };   var key = [1,2,3,4,5]; //also filled code   var lcm = myobj[key[key.length-1]].foreach(function(v) { //iterate through last/largest multiple array   var j=key[key.length-2];   while (j>=0) {     if (compare(myobj,v,j)) {  //check see if in next lower array, if yes, check next one.       j--;     }     if (j>0 && (compare(myobj,v,j+1))===true) return v;  //before loop exits, return match    }    }); return lcm; } 

i'm not sure wrong, returning undefined.

note: yes, know foreach returns undefined, , tried modifying code, , "potential infinite loop" error editor. modified code looks this:

function leastcommon ([1,5]) {   var myobj = { //this filled code, finished result looks     1: [1, 2,...,60,...10k]     2: [2, 4,...,60,...20k]     3: [3, 6,...,60,...30k]     4: [4, 8,...,60,...40k]     5: [5,10,...,60,...50k]   };   var key = [1,2,3,4,5]; //also filled code   var lcm = 0;   myobj[key[key.length-1]].foreach(function(v) { //iterate through last/largest multiple array     var j=key[key.length-2];     while (j>=0) {       if (compare(myobj,v,j)) {  //check see if in next lower array, if yes, check next one.         j--;       }       if (j>0 && (compare(myobj,v,j+1))===true) lcm = v;  //before loop exits, set lcm = v     }     }); return lcm; } 

i not use foreach since there no way exit method when find first match/failure. need keep looping. instead should regular loop every , indexof. code assumes array sorted smallest number comes first. if not, simple sort() clone of array can solve that.

//pass in arrays, assumes array sorted  function getfirstcommon (arrs) {    //loop on elements in first array    (var i=0; i<arrs[0].length; i++) {      //get value current index      var val = arrs[0][i];      //make sure every array has value      //if every not find it, returns false      var test = arrs.every( function (arr) {        //check array see if has element        return arr.indexof(val)!==-1;        });      //if find it, return current value      if (test) {        return val;      }       }    //if nothing found, return null    return null;   }    //test numbers  var nums = [     [1,2,3,4,5,6],     [2,3,4,5,6],     [3,4,5,6],     [4,5,6,7,8,9],     [6,7,8,9]  ];      console.log(getfirstcommon(nums));  //6      var nums2 = [     [1,2,3,4,5,6],     [2,3,4,5,6],     [3,4,5,6],     [4,5,6,7,8,9],     [5,7,8,9]  ];      console.log(getfirstcommon(nums2));  //5    var nums3 = [     [1,2,3,4,5,6],     [7,8,9,10,11,12],     [7,8,9,10,11,12]  ];      console.log(getfirstcommon(nums3));  //null

the code improved not check itself


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -