Skip to main content

In JavaScript you can do this:

  1. First, order your array using the JavaScript Array.sort() method, with complexity \$O(n\log(n))\$ (explained here):

     var A=[4,3,2,1,0,-3];
     A.sort(function(a, b){return a-b});
     //returns the array ordered [-3,0,1,2,3,4]
    
  2. Only iterate over the ordered array. For every value, check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

     function(A){
         for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
             if(A[i]>0 && A[i+1]!=(A[i]+1)){
                 return (A[i]+1);
             }
         }
     }
    
    First, remove duplicates if any

    var A=[4,3,2,1,0,1,-3];
    const remove_duplicates = A.filter((number, index) => {
          return A.indexOf(number) === index
    })

  1. Secondly, order your array using the JavaScript Array.sort() method, with complexity \$O(n\log(n))\$ (explained here):

     const sorted = remove_duplicates.sort(function(a, b){return a-b});
     //returns the array ordered [-3,0,1,2,3,4]
    
  2. Only iterate over the ordered array. For every value, check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

    // find smallest positive integer not in sorted greater than 0
    // return only valid number, unless all negative values else return 1
    const result = sorted.map((number, index) => {
        if (number > 0 && sorted[index + 1] !== sorted[index] + 1) {
            return sorted[index] + 1
        }
    }).filter(number => number !== undefined)[0]

    return result === undefined ? 1 : result

In JavaScript you can do this:

  1. First, order your array using the JavaScript Array.sort() method, with complexity \$O(n\log(n))\$ (explained here):

     var A=[4,3,2,1,0,-3];
     A.sort(function(a, b){return a-b});
     //returns the array ordered [-3,0,1,2,3,4]
    
  2. Only iterate over the ordered array. For every value, check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

     function(A){
         for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
             if(A[i]>0 && A[i+1]!=(A[i]+1)){
                 return (A[i]+1);
             }
         }
     }
    

In JavaScript you can do this:

  1. First, remove duplicates if any

    var A=[4,3,2,1,0,1,-3];
    const remove_duplicates = A.filter((number, index) => {
          return A.indexOf(number) === index
    })

  1. Secondly, order your array using the JavaScript Array.sort() method, with complexity \$O(n\log(n))\$ (explained here):

     const sorted = remove_duplicates.sort(function(a, b){return a-b});
     //returns the array ordered [-3,0,1,2,3,4]
    
  2. Only iterate over the ordered array. For every value, check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

    // find smallest positive integer not in sorted greater than 0
    // return only valid number, unless all negative values else return 1
    const result = sorted.map((number, index) => {
        if (number > 0 && sorted[index + 1] !== sorted[index] + 1) {
            return sorted[index] + 1
        }
    }).filter(number => number !== undefined)[0]

    return result === undefined ? 1 : result

added 62 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

In JavascriptJavaScript you can do this:

1.-First urder your array using the Javascript Array.sort() method, with complexity O(n log n)(explained here).

var A=[4,3,2,1,0,-3];
A.sort(function(a, b){return a-b});
 //returns the array ordered [-3,0,1,2,3,4]

2.- Then only iterate over the ordered array, for every value check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

function(A){
for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
   if(A[i]>0 && A[i+1]!=(A[i]+1)){
      return (A[i]+1);
    }
 }
}
  1. First, order your array using the JavaScript Array.sort() method, with complexity \$O(n\log(n))\$ (explained here):

     var A=[4,3,2,1,0,-3];
     A.sort(function(a, b){return a-b});
     //returns the array ordered [-3,0,1,2,3,4]
    
  2. Only iterate over the ordered array. For every value, check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

     function(A){
         for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
             if(A[i]>0 && A[i+1]!=(A[i]+1)){
                 return (A[i]+1);
             }
         }
     }
    

In Javascript you can do this:

1.-First urder your array using the Javascript Array.sort() method, with complexity O(n log n)(explained here).

var A=[4,3,2,1,0,-3];
A.sort(function(a, b){return a-b});
 //returns the array ordered [-3,0,1,2,3,4]

2.- Then only iterate over the ordered array, for every value check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

function(A){
for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
   if(A[i]>0 && A[i+1]!=(A[i]+1)){
      return (A[i]+1);
    }
 }
}

In JavaScript you can do this:

  1. First, order your array using the JavaScript Array.sort() method, with complexity \$O(n\log(n))\$ (explained here):

     var A=[4,3,2,1,0,-3];
     A.sort(function(a, b){return a-b});
     //returns the array ordered [-3,0,1,2,3,4]
    
  2. Only iterate over the ordered array. For every value, check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

     function(A){
         for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
             if(A[i]>0 && A[i+1]!=(A[i]+1)){
                 return (A[i]+1);
             }
         }
     }
    
Source Link

In Javascript you can do this:

1.-First urder your array using the Javascript Array.sort() method, with complexity O(n log n)(explained here).

var A=[4,3,2,1,0,-3];
A.sort(function(a, b){return a-b});
 //returns the array ordered [-3,0,1,2,3,4]

2.- Then only iterate over the ordered array, for every value check if the value is bigger than 0 and if the next element on the array is not equal to the current value + 1.

function(A){
for(var i=0;i<A.length-1;i++){// iterate until  penultimate element
   if(A[i]>0 && A[i+1]!=(A[i]+1)){
      return (A[i]+1);
    }
 }
}