Finding missing numbers in an array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












Is it possible to make this faster? Any suggestions are more than welcome



I have used JavaScript to write this code



PROBLEM




  • You will get an array of numbers.

  • Every preceding number is smaller than the one following it.



  • Some numbers will be missing, for instance:



    [-3,-2,1,5] // missing numbers are: -1,0,2,3,4


Your task is to return an array of those missing numbers:



[-1,0,2,3,4]



SOLUTION



const findTheMissing = (target) => 

// final result list
let result = ;

// array will go from min to max value present in the array
const min = target[0];
const max = target[target.length - 1];

// will maintain the track of index of target array
// will start from 2nd element of array because we need a no. to subsctract from
let pivotIndex = 1;

for (let index = min; index < max; index++)

// value to the index
let pivotValue = target[pivotIndex];

// dif of the value
let diff = pivotValue - index;

// diff means its time to move the pivot to next :P
if (diff > 0)
// not going to current index at exists in the target array
if (index === target[pivotIndex - 1])
index++;

// YO!! WE FOUND HE MISSING
result.push(index);

else
pivotIndex++;


return result; // got all missing numbers



RESULT



let source = [-5, 0, 2, 5, 7];
console.log(findTheMissing(source));
// [ -4, -3, -2, -1, 2, 3, 4, 6 ]






share|improve this question

















  • 1




    what is your current benchmark?
    – Malachi♦
    Apr 16 at 13:47










  • @Malachi Number of iterations. The lesser the better
    – Vikas Bansal
    Apr 17 at 5:25











  • how did you run your test to benchmark your code?
    – NinjaG
    Apr 22 at 5:48
















up vote
4
down vote

favorite












Is it possible to make this faster? Any suggestions are more than welcome



I have used JavaScript to write this code



PROBLEM




  • You will get an array of numbers.

  • Every preceding number is smaller than the one following it.



  • Some numbers will be missing, for instance:



    [-3,-2,1,5] // missing numbers are: -1,0,2,3,4


Your task is to return an array of those missing numbers:



[-1,0,2,3,4]



SOLUTION



const findTheMissing = (target) => 

// final result list
let result = ;

// array will go from min to max value present in the array
const min = target[0];
const max = target[target.length - 1];

// will maintain the track of index of target array
// will start from 2nd element of array because we need a no. to subsctract from
let pivotIndex = 1;

for (let index = min; index < max; index++)

// value to the index
let pivotValue = target[pivotIndex];

// dif of the value
let diff = pivotValue - index;

// diff means its time to move the pivot to next :P
if (diff > 0)
// not going to current index at exists in the target array
if (index === target[pivotIndex - 1])
index++;

// YO!! WE FOUND HE MISSING
result.push(index);

else
pivotIndex++;


return result; // got all missing numbers



RESULT



let source = [-5, 0, 2, 5, 7];
console.log(findTheMissing(source));
// [ -4, -3, -2, -1, 2, 3, 4, 6 ]






share|improve this question

















  • 1




    what is your current benchmark?
    – Malachi♦
    Apr 16 at 13:47










  • @Malachi Number of iterations. The lesser the better
    – Vikas Bansal
    Apr 17 at 5:25











  • how did you run your test to benchmark your code?
    – NinjaG
    Apr 22 at 5:48












up vote
4
down vote

favorite









up vote
4
down vote

favorite











Is it possible to make this faster? Any suggestions are more than welcome



I have used JavaScript to write this code



PROBLEM




  • You will get an array of numbers.

  • Every preceding number is smaller than the one following it.



  • Some numbers will be missing, for instance:



    [-3,-2,1,5] // missing numbers are: -1,0,2,3,4


Your task is to return an array of those missing numbers:



[-1,0,2,3,4]



SOLUTION



const findTheMissing = (target) => 

// final result list
let result = ;

// array will go from min to max value present in the array
const min = target[0];
const max = target[target.length - 1];

// will maintain the track of index of target array
// will start from 2nd element of array because we need a no. to subsctract from
let pivotIndex = 1;

for (let index = min; index < max; index++)

// value to the index
let pivotValue = target[pivotIndex];

// dif of the value
let diff = pivotValue - index;

// diff means its time to move the pivot to next :P
if (diff > 0)
// not going to current index at exists in the target array
if (index === target[pivotIndex - 1])
index++;

// YO!! WE FOUND HE MISSING
result.push(index);

else
pivotIndex++;


return result; // got all missing numbers



RESULT



let source = [-5, 0, 2, 5, 7];
console.log(findTheMissing(source));
// [ -4, -3, -2, -1, 2, 3, 4, 6 ]






share|improve this question













Is it possible to make this faster? Any suggestions are more than welcome



I have used JavaScript to write this code



PROBLEM




  • You will get an array of numbers.

  • Every preceding number is smaller than the one following it.



  • Some numbers will be missing, for instance:



    [-3,-2,1,5] // missing numbers are: -1,0,2,3,4


Your task is to return an array of those missing numbers:



[-1,0,2,3,4]



SOLUTION



const findTheMissing = (target) => 

// final result list
let result = ;

// array will go from min to max value present in the array
const min = target[0];
const max = target[target.length - 1];

// will maintain the track of index of target array
// will start from 2nd element of array because we need a no. to subsctract from
let pivotIndex = 1;

for (let index = min; index < max; index++)

// value to the index
let pivotValue = target[pivotIndex];

// dif of the value
let diff = pivotValue - index;

// diff means its time to move the pivot to next :P
if (diff > 0)
// not going to current index at exists in the target array
if (index === target[pivotIndex - 1])
index++;

// YO!! WE FOUND HE MISSING
result.push(index);

else
pivotIndex++;


return result; // got all missing numbers



RESULT



let source = [-5, 0, 2, 5, 7];
console.log(findTheMissing(source));
// [ -4, -3, -2, -1, 2, 3, 4, 6 ]








share|improve this question












share|improve this question




share|improve this question








edited Apr 16 at 9:16
























asked Apr 16 at 7:20









Vikas Bansal

1265




1265







  • 1




    what is your current benchmark?
    – Malachi♦
    Apr 16 at 13:47










  • @Malachi Number of iterations. The lesser the better
    – Vikas Bansal
    Apr 17 at 5:25











  • how did you run your test to benchmark your code?
    – NinjaG
    Apr 22 at 5:48












  • 1




    what is your current benchmark?
    – Malachi♦
    Apr 16 at 13:47










  • @Malachi Number of iterations. The lesser the better
    – Vikas Bansal
    Apr 17 at 5:25











  • how did you run your test to benchmark your code?
    – NinjaG
    Apr 22 at 5:48







1




1




what is your current benchmark?
– Malachi♦
Apr 16 at 13:47




what is your current benchmark?
– Malachi♦
Apr 16 at 13:47












@Malachi Number of iterations. The lesser the better
– Vikas Bansal
Apr 17 at 5:25





@Malachi Number of iterations. The lesser the better
– Vikas Bansal
Apr 17 at 5:25













how did you run your test to benchmark your code?
– NinjaG
Apr 22 at 5:48




how did you run your test to benchmark your code?
– NinjaG
Apr 22 at 5:48










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










I think, there is an error:



If I enter the values:



[-3,-2,1,5]


I get the result:



[-2,0,2,3,4]


but it should be:



[-1,0,2,3,4]


The error occurs when there are two adjacent numbers in the array (here -3 and -2).




Naming:



Index is not an index, but the actual value in the valid sequence (from min to max). I would call it value or curValue or something like that.



I don't like the name target either, because the target is actually the sequence of numbers from min to max. input would be better IMO.




Analysis



Basically you want to compare two sequences of numbers index by index. If they differ then save the target value, if not then increment to the next value of the input array. The index of the target sequence (here from min to max) is always incremented. Instead of the target sequence you can just increment a value starting from input[0] and ending on input.[input.length - 1].



All in all it could be done something like this:



function findMissing(input) 

var result = ;

for (var inputIndex = 0, targetValue = input[0]; targetValue <= input[input.length - 1]; targetValue++)
if (input[inputIndex] != targetValue)
result.push(targetValue);

else
inputIndex++;



return result;






share|improve this answer






























    up vote
    1
    down vote













    Foreknowledge of result length



    This type of problem has a short cut because you can know the size of the resulting array by just inspecting the first and last items. You can then use the calculated result length as an exit condition.



    In the best result you only have to inspect the first and last value and exit without iteration if there are no missing values.



    The worst you will have to count over all the values in between the first and last.



    The resulting algorithm is very efficient



    function missing(arr) 
    const result = ;
    if (arr.length <= 1) return result
    var i = 1, val = arr[0] + 1;
    const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
    while (result.length < count)
    while (arr[i] !== val) result.push(val++)
    i++;
    val++;

    return result;



    The snippet shows the number of iterations to find the missing values.






    var iterations;
    function missing(arr)
    var ic = 0; // iteration counter
    const result = ;
    if (arr.length <= 1) return result
    var i = 1, val = arr[0] + 1;
    const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
    while (result.length < count)
    ic = 0;
    while (arr[i] !== val)
    result.push(val++)
    ic ++; // Count once for each inner iteration step

    i++;
    val++;
    iterations += ic ? ic : 1; // count inner or one for outer loop

    return result;


    function doIt(arr)
    iterations = 0;
    console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


    doIt([0,9])
    doIt([0,2,3,8,9])
    doIt([0,2,3,6,8,9])
    doIt([0,2,4,5,6,7,8,9])
    doIt([0,2,3,4,5,6,7,8,9])
    doIt([0,1,2,3,4,5,6,7,9])
    doIt([0,1,2,3,4,5,6,7,8,9])








    share|improve this answer






























      up vote
      -2
      down vote













      No rocket science:



      • Do Lesser operations and your program is faster

      • No Loop inside Loops and Time complexity is less

      • Don't initialize variables in a loop instead assign them in variables out of loop

      • Enhance readability using KISS

      • Use Bit-wise operators and gain more speed, I haven't used

      CODE



      function findMissingNumbers(array) 
      const arraySize = array.length;
      const arr = ;
      let i = 0;
      let j = array[0];
      let jSize = array[arraySize - 1];
      while (j < jSize)
      (array[i] === j) ? (i += 1) : (arr.push(j));
      j++;

      return arr;






      share|improve this answer























      • What exactly you want to understand.
        – Shubhendu Vaid
        Apr 16 at 11:39






      • 3




        You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
        – Martin R
        Apr 16 at 13:02










      • @MartinR please check now. Hope i am able to keep it clean and simple. :)
        – Shubhendu Vaid
        Apr 16 at 14:00







      • 2




        While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
        – Simon Forsberg♦
        Apr 16 at 14:18










      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192172%2ffinding-missing-numbers-in-an-array%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      I think, there is an error:



      If I enter the values:



      [-3,-2,1,5]


      I get the result:



      [-2,0,2,3,4]


      but it should be:



      [-1,0,2,3,4]


      The error occurs when there are two adjacent numbers in the array (here -3 and -2).




      Naming:



      Index is not an index, but the actual value in the valid sequence (from min to max). I would call it value or curValue or something like that.



      I don't like the name target either, because the target is actually the sequence of numbers from min to max. input would be better IMO.




      Analysis



      Basically you want to compare two sequences of numbers index by index. If they differ then save the target value, if not then increment to the next value of the input array. The index of the target sequence (here from min to max) is always incremented. Instead of the target sequence you can just increment a value starting from input[0] and ending on input.[input.length - 1].



      All in all it could be done something like this:



      function findMissing(input) 

      var result = ;

      for (var inputIndex = 0, targetValue = input[0]; targetValue <= input[input.length - 1]; targetValue++)
      if (input[inputIndex] != targetValue)
      result.push(targetValue);

      else
      inputIndex++;



      return result;






      share|improve this answer



























        up vote
        3
        down vote



        accepted










        I think, there is an error:



        If I enter the values:



        [-3,-2,1,5]


        I get the result:



        [-2,0,2,3,4]


        but it should be:



        [-1,0,2,3,4]


        The error occurs when there are two adjacent numbers in the array (here -3 and -2).




        Naming:



        Index is not an index, but the actual value in the valid sequence (from min to max). I would call it value or curValue or something like that.



        I don't like the name target either, because the target is actually the sequence of numbers from min to max. input would be better IMO.




        Analysis



        Basically you want to compare two sequences of numbers index by index. If they differ then save the target value, if not then increment to the next value of the input array. The index of the target sequence (here from min to max) is always incremented. Instead of the target sequence you can just increment a value starting from input[0] and ending on input.[input.length - 1].



        All in all it could be done something like this:



        function findMissing(input) 

        var result = ;

        for (var inputIndex = 0, targetValue = input[0]; targetValue <= input[input.length - 1]; targetValue++)
        if (input[inputIndex] != targetValue)
        result.push(targetValue);

        else
        inputIndex++;



        return result;






        share|improve this answer

























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          I think, there is an error:



          If I enter the values:



          [-3,-2,1,5]


          I get the result:



          [-2,0,2,3,4]


          but it should be:



          [-1,0,2,3,4]


          The error occurs when there are two adjacent numbers in the array (here -3 and -2).




          Naming:



          Index is not an index, but the actual value in the valid sequence (from min to max). I would call it value or curValue or something like that.



          I don't like the name target either, because the target is actually the sequence of numbers from min to max. input would be better IMO.




          Analysis



          Basically you want to compare two sequences of numbers index by index. If they differ then save the target value, if not then increment to the next value of the input array. The index of the target sequence (here from min to max) is always incremented. Instead of the target sequence you can just increment a value starting from input[0] and ending on input.[input.length - 1].



          All in all it could be done something like this:



          function findMissing(input) 

          var result = ;

          for (var inputIndex = 0, targetValue = input[0]; targetValue <= input[input.length - 1]; targetValue++)
          if (input[inputIndex] != targetValue)
          result.push(targetValue);

          else
          inputIndex++;



          return result;






          share|improve this answer















          I think, there is an error:



          If I enter the values:



          [-3,-2,1,5]


          I get the result:



          [-2,0,2,3,4]


          but it should be:



          [-1,0,2,3,4]


          The error occurs when there are two adjacent numbers in the array (here -3 and -2).




          Naming:



          Index is not an index, but the actual value in the valid sequence (from min to max). I would call it value or curValue or something like that.



          I don't like the name target either, because the target is actually the sequence of numbers from min to max. input would be better IMO.




          Analysis



          Basically you want to compare two sequences of numbers index by index. If they differ then save the target value, if not then increment to the next value of the input array. The index of the target sequence (here from min to max) is always incremented. Instead of the target sequence you can just increment a value starting from input[0] and ending on input.[input.length - 1].



          All in all it could be done something like this:



          function findMissing(input) 

          var result = ;

          for (var inputIndex = 0, targetValue = input[0]; targetValue <= input[input.length - 1]; targetValue++)
          if (input[inputIndex] != targetValue)
          result.push(targetValue);

          else
          inputIndex++;



          return result;







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Apr 23 at 4:38


























          answered Apr 21 at 14:55









          Henrik Hansen

          3,8481417




          3,8481417






















              up vote
              1
              down vote













              Foreknowledge of result length



              This type of problem has a short cut because you can know the size of the resulting array by just inspecting the first and last items. You can then use the calculated result length as an exit condition.



              In the best result you only have to inspect the first and last value and exit without iteration if there are no missing values.



              The worst you will have to count over all the values in between the first and last.



              The resulting algorithm is very efficient



              function missing(arr) 
              const result = ;
              if (arr.length <= 1) return result
              var i = 1, val = arr[0] + 1;
              const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
              while (result.length < count)
              while (arr[i] !== val) result.push(val++)
              i++;
              val++;

              return result;



              The snippet shows the number of iterations to find the missing values.






              var iterations;
              function missing(arr)
              var ic = 0; // iteration counter
              const result = ;
              if (arr.length <= 1) return result
              var i = 1, val = arr[0] + 1;
              const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
              while (result.length < count)
              ic = 0;
              while (arr[i] !== val)
              result.push(val++)
              ic ++; // Count once for each inner iteration step

              i++;
              val++;
              iterations += ic ? ic : 1; // count inner or one for outer loop

              return result;


              function doIt(arr)
              iterations = 0;
              console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


              doIt([0,9])
              doIt([0,2,3,8,9])
              doIt([0,2,3,6,8,9])
              doIt([0,2,4,5,6,7,8,9])
              doIt([0,2,3,4,5,6,7,8,9])
              doIt([0,1,2,3,4,5,6,7,9])
              doIt([0,1,2,3,4,5,6,7,8,9])








              share|improve this answer



























                up vote
                1
                down vote













                Foreknowledge of result length



                This type of problem has a short cut because you can know the size of the resulting array by just inspecting the first and last items. You can then use the calculated result length as an exit condition.



                In the best result you only have to inspect the first and last value and exit without iteration if there are no missing values.



                The worst you will have to count over all the values in between the first and last.



                The resulting algorithm is very efficient



                function missing(arr) 
                const result = ;
                if (arr.length <= 1) return result
                var i = 1, val = arr[0] + 1;
                const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                while (result.length < count)
                while (arr[i] !== val) result.push(val++)
                i++;
                val++;

                return result;



                The snippet shows the number of iterations to find the missing values.






                var iterations;
                function missing(arr)
                var ic = 0; // iteration counter
                const result = ;
                if (arr.length <= 1) return result
                var i = 1, val = arr[0] + 1;
                const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                while (result.length < count)
                ic = 0;
                while (arr[i] !== val)
                result.push(val++)
                ic ++; // Count once for each inner iteration step

                i++;
                val++;
                iterations += ic ? ic : 1; // count inner or one for outer loop

                return result;


                function doIt(arr)
                iterations = 0;
                console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


                doIt([0,9])
                doIt([0,2,3,8,9])
                doIt([0,2,3,6,8,9])
                doIt([0,2,4,5,6,7,8,9])
                doIt([0,2,3,4,5,6,7,8,9])
                doIt([0,1,2,3,4,5,6,7,9])
                doIt([0,1,2,3,4,5,6,7,8,9])








                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Foreknowledge of result length



                  This type of problem has a short cut because you can know the size of the resulting array by just inspecting the first and last items. You can then use the calculated result length as an exit condition.



                  In the best result you only have to inspect the first and last value and exit without iteration if there are no missing values.



                  The worst you will have to count over all the values in between the first and last.



                  The resulting algorithm is very efficient



                  function missing(arr) 
                  const result = ;
                  if (arr.length <= 1) return result
                  var i = 1, val = arr[0] + 1;
                  const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                  while (result.length < count)
                  while (arr[i] !== val) result.push(val++)
                  i++;
                  val++;

                  return result;



                  The snippet shows the number of iterations to find the missing values.






                  var iterations;
                  function missing(arr)
                  var ic = 0; // iteration counter
                  const result = ;
                  if (arr.length <= 1) return result
                  var i = 1, val = arr[0] + 1;
                  const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                  while (result.length < count)
                  ic = 0;
                  while (arr[i] !== val)
                  result.push(val++)
                  ic ++; // Count once for each inner iteration step

                  i++;
                  val++;
                  iterations += ic ? ic : 1; // count inner or one for outer loop

                  return result;


                  function doIt(arr)
                  iterations = 0;
                  console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


                  doIt([0,9])
                  doIt([0,2,3,8,9])
                  doIt([0,2,3,6,8,9])
                  doIt([0,2,4,5,6,7,8,9])
                  doIt([0,2,3,4,5,6,7,8,9])
                  doIt([0,1,2,3,4,5,6,7,9])
                  doIt([0,1,2,3,4,5,6,7,8,9])








                  share|improve this answer















                  Foreknowledge of result length



                  This type of problem has a short cut because you can know the size of the resulting array by just inspecting the first and last items. You can then use the calculated result length as an exit condition.



                  In the best result you only have to inspect the first and last value and exit without iteration if there are no missing values.



                  The worst you will have to count over all the values in between the first and last.



                  The resulting algorithm is very efficient



                  function missing(arr) 
                  const result = ;
                  if (arr.length <= 1) return result
                  var i = 1, val = arr[0] + 1;
                  const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                  while (result.length < count)
                  while (arr[i] !== val) result.push(val++)
                  i++;
                  val++;

                  return result;



                  The snippet shows the number of iterations to find the missing values.






                  var iterations;
                  function missing(arr)
                  var ic = 0; // iteration counter
                  const result = ;
                  if (arr.length <= 1) return result
                  var i = 1, val = arr[0] + 1;
                  const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                  while (result.length < count)
                  ic = 0;
                  while (arr[i] !== val)
                  result.push(val++)
                  ic ++; // Count once for each inner iteration step

                  i++;
                  val++;
                  iterations += ic ? ic : 1; // count inner or one for outer loop

                  return result;


                  function doIt(arr)
                  iterations = 0;
                  console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


                  doIt([0,9])
                  doIt([0,2,3,8,9])
                  doIt([0,2,3,6,8,9])
                  doIt([0,2,4,5,6,7,8,9])
                  doIt([0,2,3,4,5,6,7,8,9])
                  doIt([0,1,2,3,4,5,6,7,9])
                  doIt([0,1,2,3,4,5,6,7,8,9])








                  var iterations;
                  function missing(arr)
                  var ic = 0; // iteration counter
                  const result = ;
                  if (arr.length <= 1) return result
                  var i = 1, val = arr[0] + 1;
                  const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                  while (result.length < count)
                  ic = 0;
                  while (arr[i] !== val)
                  result.push(val++)
                  ic ++; // Count once for each inner iteration step

                  i++;
                  val++;
                  iterations += ic ? ic : 1; // count inner or one for outer loop

                  return result;


                  function doIt(arr)
                  iterations = 0;
                  console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


                  doIt([0,9])
                  doIt([0,2,3,8,9])
                  doIt([0,2,3,6,8,9])
                  doIt([0,2,4,5,6,7,8,9])
                  doIt([0,2,3,4,5,6,7,8,9])
                  doIt([0,1,2,3,4,5,6,7,9])
                  doIt([0,1,2,3,4,5,6,7,8,9])





                  var iterations;
                  function missing(arr)
                  var ic = 0; // iteration counter
                  const result = ;
                  if (arr.length <= 1) return result
                  var i = 1, val = arr[0] + 1;
                  const count = ((arr[arr.length - 1]) - val) - (arr.length - 2);
                  while (result.length < count)
                  ic = 0;
                  while (arr[i] !== val)
                  result.push(val++)
                  ic ++; // Count once for each inner iteration step

                  i++;
                  val++;
                  iterations += ic ? ic : 1; // count inner or one for outer loop

                  return result;


                  function doIt(arr)
                  iterations = 0;
                  console.log("Array [" + arr.join("") + "] missing ["+ missing(arr).join("") + "] in " + iterations + " iterations.");


                  doIt([0,9])
                  doIt([0,2,3,8,9])
                  doIt([0,2,3,6,8,9])
                  doIt([0,2,4,5,6,7,8,9])
                  doIt([0,2,3,4,5,6,7,8,9])
                  doIt([0,1,2,3,4,5,6,7,9])
                  doIt([0,1,2,3,4,5,6,7,8,9])






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Apr 17 at 12:03


























                  answered Apr 16 at 12:39









                  Blindman67

                  5,3611320




                  5,3611320




















                      up vote
                      -2
                      down vote













                      No rocket science:



                      • Do Lesser operations and your program is faster

                      • No Loop inside Loops and Time complexity is less

                      • Don't initialize variables in a loop instead assign them in variables out of loop

                      • Enhance readability using KISS

                      • Use Bit-wise operators and gain more speed, I haven't used

                      CODE



                      function findMissingNumbers(array) 
                      const arraySize = array.length;
                      const arr = ;
                      let i = 0;
                      let j = array[0];
                      let jSize = array[arraySize - 1];
                      while (j < jSize)
                      (array[i] === j) ? (i += 1) : (arr.push(j));
                      j++;

                      return arr;






                      share|improve this answer























                      • What exactly you want to understand.
                        – Shubhendu Vaid
                        Apr 16 at 11:39






                      • 3




                        You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                        – Martin R
                        Apr 16 at 13:02










                      • @MartinR please check now. Hope i am able to keep it clean and simple. :)
                        – Shubhendu Vaid
                        Apr 16 at 14:00







                      • 2




                        While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
                        – Simon Forsberg♦
                        Apr 16 at 14:18














                      up vote
                      -2
                      down vote













                      No rocket science:



                      • Do Lesser operations and your program is faster

                      • No Loop inside Loops and Time complexity is less

                      • Don't initialize variables in a loop instead assign them in variables out of loop

                      • Enhance readability using KISS

                      • Use Bit-wise operators and gain more speed, I haven't used

                      CODE



                      function findMissingNumbers(array) 
                      const arraySize = array.length;
                      const arr = ;
                      let i = 0;
                      let j = array[0];
                      let jSize = array[arraySize - 1];
                      while (j < jSize)
                      (array[i] === j) ? (i += 1) : (arr.push(j));
                      j++;

                      return arr;






                      share|improve this answer























                      • What exactly you want to understand.
                        – Shubhendu Vaid
                        Apr 16 at 11:39






                      • 3




                        You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                        – Martin R
                        Apr 16 at 13:02










                      • @MartinR please check now. Hope i am able to keep it clean and simple. :)
                        – Shubhendu Vaid
                        Apr 16 at 14:00







                      • 2




                        While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
                        – Simon Forsberg♦
                        Apr 16 at 14:18












                      up vote
                      -2
                      down vote










                      up vote
                      -2
                      down vote









                      No rocket science:



                      • Do Lesser operations and your program is faster

                      • No Loop inside Loops and Time complexity is less

                      • Don't initialize variables in a loop instead assign them in variables out of loop

                      • Enhance readability using KISS

                      • Use Bit-wise operators and gain more speed, I haven't used

                      CODE



                      function findMissingNumbers(array) 
                      const arraySize = array.length;
                      const arr = ;
                      let i = 0;
                      let j = array[0];
                      let jSize = array[arraySize - 1];
                      while (j < jSize)
                      (array[i] === j) ? (i += 1) : (arr.push(j));
                      j++;

                      return arr;






                      share|improve this answer















                      No rocket science:



                      • Do Lesser operations and your program is faster

                      • No Loop inside Loops and Time complexity is less

                      • Don't initialize variables in a loop instead assign them in variables out of loop

                      • Enhance readability using KISS

                      • Use Bit-wise operators and gain more speed, I haven't used

                      CODE



                      function findMissingNumbers(array) 
                      const arraySize = array.length;
                      const arr = ;
                      let i = 0;
                      let j = array[0];
                      let jSize = array[arraySize - 1];
                      while (j < jSize)
                      (array[i] === j) ? (i += 1) : (arr.push(j));
                      j++;

                      return arr;







                      share|improve this answer















                      share|improve this answer



                      share|improve this answer








                      edited Apr 16 at 14:56


























                      answered Apr 16 at 9:59









                      Shubhendu Vaid

                      1497




                      1497











                      • What exactly you want to understand.
                        – Shubhendu Vaid
                        Apr 16 at 11:39






                      • 3




                        You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                        – Martin R
                        Apr 16 at 13:02










                      • @MartinR please check now. Hope i am able to keep it clean and simple. :)
                        – Shubhendu Vaid
                        Apr 16 at 14:00







                      • 2




                        While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
                        – Simon Forsberg♦
                        Apr 16 at 14:18
















                      • What exactly you want to understand.
                        – Shubhendu Vaid
                        Apr 16 at 11:39






                      • 3




                        You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                        – Martin R
                        Apr 16 at 13:02










                      • @MartinR please check now. Hope i am able to keep it clean and simple. :)
                        – Shubhendu Vaid
                        Apr 16 at 14:00







                      • 2




                        While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
                        – Simon Forsberg♦
                        Apr 16 at 14:18















                      What exactly you want to understand.
                      – Shubhendu Vaid
                      Apr 16 at 11:39




                      What exactly you want to understand.
                      – Shubhendu Vaid
                      Apr 16 at 11:39




                      3




                      3




                      You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                      – Martin R
                      Apr 16 at 13:02




                      You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process.
                      – Martin R
                      Apr 16 at 13:02












                      @MartinR please check now. Hope i am able to keep it clean and simple. :)
                      – Shubhendu Vaid
                      Apr 16 at 14:00





                      @MartinR please check now. Hope i am able to keep it clean and simple. :)
                      – Shubhendu Vaid
                      Apr 16 at 14:00





                      2




                      2




                      While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
                      – Simon Forsberg♦
                      Apr 16 at 14:18




                      While your points here helps a bit, it is not always entirely correct and it's not clear how you arrived at the smaller code. By the way, the original code did not have any loops inside loops.
                      – Simon Forsberg♦
                      Apr 16 at 14:18












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192172%2ffinding-missing-numbers-in-an-array%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Greedy Best First Search implementation in Rust

                      Function to Return a JSON Like Objects Using VBA Collections and Arrays

                      C++11 CLH Lock Implementation