1 Reply Latest reply on May 31, 2015 4:44 AM by Peter Kahrel

    Find the missing number in array

    Level 1

      I need to find the missing number in array.

       

       

      Here is my code

       

       

      var txt_name=selFolder+"/Xref.txt"

      var txt_file=File(txt_name);

      txt_file.open("w");

      var ktId=new Array();

      var doc = app.activeDocument;

      var xmlTag = doc.xmlElements[0];

      var vTag = xmlTag.evaluateXPathExpression("//CITATION")

      for(var i=vTag.length-1;i>=0;i--)

      {

          try

          {

           ktId.push (vTag[i].xmlContent.contents+"\n")

           }

          catch(e){}

      }

       

      txt_file.writeln(arrayCompress(ktId));

       

      function arrayCompress(array){ 

          var str = array.sort().join('\r')+'\r'; 

          var str = str.replace(/([^\r]+\r)(\1)+/g,'$1'); 

          str = str.replace(/\r$/,''); 

          return str.split('\r'); 

      }

       

      OUTPUT : 1,2,3,5,6,7,10

       

       

      Please advice

        • 1. Re: Find the missing number in array
          Peter Kahrel Adobe Community Professional & MVP

          This returns an array of missing numbers:

           

          function findMissingNumbers (list){
            var missing = [];
            if (list.length > 1 && list.length !== list[list.length-1]) {
              var present = []; 
              for (var i = 0; i < list.length; i++) {
                present[list[i]] = true;
              }
              var last = list[list.length-1];
              for (i = 1; i < last; i++) {
                if (!present[i]) {
                  missing.push (i);
                }
              }
            }
            return missing;
          }
          
          

           

          Peter