missing integer codility Javascript
Asked Answered
H

38

7

Write a function:

function solution(A); 

that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer (greater than 0) that does not occur in A. For example, given:

A[0] = 1   
A[1] = 3   
A[2] = 6   
A[3] = 4   
A[4] = 1   
A[5] = 2

the function should return 5. Assume that:

• N is an integer within the range [1..100,000]; • each element of array A is an integer within the range

[−2,147,483,648..2,147,483,647].

Complexity: • expected worst-case time complexity is O(N); • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

My Answer is 100% WRONG! What is wrong with it? First let me state the obvious errors

  • return value - i return 0, because there is no indication of what to return, if there is no missing integer.

Assumptions I made that may be wrong

  • returns the minimal positive integer (greater than 0) that does not occur in A. Here I do not check for negative values

my code, which works on own test cases, and works on negative numbers too, got 0%.

function solution(A) {

    // write your code in JavaScript (Node.js 0.12)
    A.sort();
    var a_length = A.length;

    for(var i = 0; i < a_length; i++){

        // if i is 0 - 1 = -1 then do not do the following
        // if i is 1 - 1 - 0 then do the follow
        // if i >= 0 then do the following
        if(i - 1 >= 0){

            // remember above there is a A.sort() so it 
            // looks like this
            // A[0] = 1
            // A[1] = 1
            // A[2] = 2
            // A[3] = 3
            // A[4] = 4
            // A[5] = 6

            // if A[1] is 1 and A[1-1 = 0] is 1 then this is 1>1 false 
            // if A[2] is 2 and A[2-1 = 1] is 1 then this is 1>1 false  
            // if A[3] is 3 and A[3-1 = 2] is 2 then this is 1>1 false  
            // if A[4] is 4 and A[4-1 = 3] is 3 then this is 1>1 false  
            // if A[5] is 6 and A[5-1 = 4] is 4 then this is 2>1 true return A[i - 1] + 1 where A[5 - 1 = 4] is 4 + 1 is 5. 5 is returned.
            if(A[i] - A[i - 1] > 1){
                return A[i - 1] + 1;
            }
        }
    }

    // this does not check for negative
    // returns the minimal positive integer (greater than 0
    // this is a return no minimal positive integer found
    return 0;
}

Everything is wrong, example test result:

simple simple test 0.072 s WRONG ANSWER got 3 expected 1

Why does it work for me and not for them.

Hammerskjold answered 9/6, 2015 at 6:57 Comment(2)
just why don't you simply change the algorithm, find the smallest number and keep on adding one to it, say that to be variable "number" then check if that "number" is present in the given set of array and if not, then that would be your solution and of course the complexity is high but it would get you a new direction to think and ya keep a not of highest number in the array, so that when the "number" is equal to highest number you can return zero when it is requiredWaxler
In the original, admittedly old, question, the OP asked about what was wrong in his / her solution. A lot of nice contributions here, but nobody explicitly pointing out the errors in the original: 1. Array.prototype.sort by default sorts in ASCII character order 2. the OP's default return is 0, which is not a positive integerPortia
U
35
function solution(A) {
        var min = 1;
        A.sort(function(a,b){
           // Sort the array explicit way
           return a - b; 
        });

        for (var i in A) {
            if (A[i] > -1 && A[i] == min) {
                    min++;
            }
        }

        return min;
}
Univalence answered 3/3, 2016 at 22:24 Comment(1)
If you add this line const positiveMember = A.filter(a => a > -1); after sorting and in the for loop use positiveMember instead of A - it would minimize the complexity - considering the loop will run only for the positive integers. So no need for the first condition in if.Hathcock
M
23

There is my solution with JavaScript for Codility MissingInteger (got 100/100)

function solution(A) {
    const len = A.length;
    const hash = {};
    for (let i = 0; i < len; i++) {
        // here we are making an object with all 
        // numbers in a given array as object keys and values
        // if 0 is given as possible digit we could assing 
        // hash[A[i]] = true; or any truthy value
        hash[A[i]] = A[i];
    }
    for (let i = 1; i < 1000002; i++) {
        // here we are trying to find any number 
        // between 1 and 1000001 (given constraints) 
        // which do not exists in an object
        // if the number is not in the object that's our missing one
        if(!hash[i]) return i;
    }
    return 1;
}
Megan answered 18/6, 2018 at 10:18 Comment(1)
Great solution! Easy to understand and scores 100/100Singles
J
11

For this problem I like to start by sorting the given array. I then iterate through the sorted array with a reduce. I give the reduce an accumulator acc initially equal to 1 (that's what the 1 after the comma is for). Only when the element val is equal to the accumulator do I increment the accumulator. Otherwise, I return the accumulator as is. When I can no longer find an element in the array equal to the accumulator, that accumulator is the lowest missing positive integer.

const solution = A => {
    A.sort((a, b) => a - b);
    return A.reduce((acc, val) => acc === val ? acc + 1 : acc, 1);
}

I know this question's been around for a while but I hope this answer is useful for someone. I use Array.prototype.sort(), Array.prototype.reduce(), and a ternary in this answer. A knowledge of those patterns should give more insight into this answer.

Joist answered 7/1, 2019 at 18:17 Comment(0)
I
8
function solution(A) {
  const set = new Set(A);
  let i = 1;
  while (true) {
    if (!set.has(i)) return i;
    i++;
  }
}
Ianthe answered 15/1, 2021 at 2:56 Comment(2)
This solution is insultingly simple and gets 100%, +1Fucus
Initially I used A.includes() within a for loop. But that only got me 50% performance. I didn't realise Set was so much more performant!Tartuffery
C
4

function solution(A) {
        A.sort(function(a,b){
           // Sort the array explicit way
           return a - b; 
        });
        return A.reduce((prev, next)=> {
            if(next > -1 && next === prev) {
                prev++;
            }
            return prev;
        }, 1);
     ;
}
Curdle answered 21/11, 2017 at 18:50 Comment(0)
B
4

My JS solution got 100 across the board. Basically, I generate a new array whose keys will be the values of the original array and set each to some truthy value. This does two things: it takes the negative values out of the iteration loop of the new array, and also allows you to loop from the smallest value up and return the first index that gives you undefined.

function solution(A) {
    orderedArr = [];
    for (let i = 0; i < A.length; i++) {
        if (!orderedArr[A[i]]) {
            orderedArr[A[i]] = true;
         }
    }
    if (orderedArr.length === 0) return 1;
    for (let i = 1; i < orderedArr.length; i++) {
        if (!orderedArr[i]) {
            return i;
        }
    }
    return orderedArr.length;
}
Blakely answered 12/5, 2019 at 1:29 Comment(0)
H
4

My final solution :

function solution(A) {
    A = [...new Set(A)];
    let min = 1;
    A.sort((x,y) => x-y);
    for (i in A) {
       if (A[i] > -1 && A[i] == min) { min ++;}
    }
return min;
}

I find it important to remove the duplicates for an array that is huge and then sort it out this way when you fund the solution the execution would stop.

test : solution([1,2,5,7,8,3,4,2,1,9,3,3,3,3,3,3,3,3,3,3,3,3,33,3,3,3,36,-2,-1,-1,6,10,12,13,13,13,13,15,14,17,10]) -> result 11;

Result

Hieronymus answered 6/8, 2020 at 9:9 Comment(0)
B
2
function solution(A) {
// write your code in JavaScript (Node.js 6.4.0)
var b = A.sort(function(a,b){return a-b});
var length = b.length;
var min = b[0];
var max = b[length-1];
if (max<=0){return 1;}
if (min<=0 && max==1){return 2;}
if (min>1){return 1;}
if (min >=0){
    for(var i=0; i<length; i++){
       if(b[i+1]- b[i] > 1){
           return b[i]+1;
       }
    }
}
if (min<=0 && max>=0){
    for(var i=0; i<length; i++){
        if(b[i]>0 && b[i-1]<=0){
            if(b[i]-0>1){
               return 1; 
            }
            if(b[i+1]-b[i]>1){
                return b[i]+1;
            }
        }
        if(b[i]>0){
            if(b[i+1]- b[i] > 1){
               return b[i]+1;
            }
        }
    }
}
return max+1;

}

Biller answered 13/9, 2017 at 9:31 Comment(1)
Please explain how this answers the question instead of just pasting code.Filomenafiloplume
O
2

Score 100 out of 100:

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)
    const B = Array.from(new Set(A)).filter(v => v > 0).sort((a,b) => a - b);
    //First create a unique Array by creating a new set, 
    //which then we filter on values greater than 0. 
    //We sort with an explicit function, 
    //otherwise our integers are converted to strings 
    //and sorting will be based on first character rather than on value

    let i = 1; //set our initial value

    for(const val of B){ //iterate over the unique values
        if(i < val){
            return i; 
            //if val is greater than i, that means the value of i was
            //not present in our  array hence we return it
        }
        i++; //if the value was present we go to the next value, hence increase by 1
    }
    return i; 
    //if we went over the whole array, that means all values were present;
    //hence the new value of i, which equals to the highest value of the array + 1 is the correct answer.
    //Since the loop would not run in case of an empty array (which due to the filter also occurs in case of negative values), 
    //the value of i is still 1 and the correct answer.
}

If you do not want to use set (for example, because you are using old ES version) you can filter like this:

A.filter((v,i,arr) => v > 0 && arr.indexOf(v) === i)
Ommatidium answered 12/5, 2020 at 12:48 Comment(0)
B
1

My solution:

function solution(A) {
// write your code in JavaScript (Node.js 6.4.0)
var B = A.sort(function(a,b){return a-b});
if(A.length == 1) {
    if(A[0] > 0) {
        if(A[0]>1) {
            return 1
        } else {
            return A[0]+ 1;    
        }
    } else {
        return 1   
    }
} else if(A.length > 1) {
    let max = Math.max.apply(null,A);
    let min = Math.min.apply(null,A);
    if(max > 0) {
        if(B[0]-0 > 1) { //for arrays that have a minimum higher than 1
            return 1
        }
        for(i=0;i<B.length-1;i++) { 
            if(B[i+1]- B[i] > 1){ //check the difference between next and current number, we also ignore the case of [x,-1,1,x2], since if a 0 is omitted, 1-(-1) will make 0 the highest missing number
                if(B[i] == -1 && B[i+1] == 1) {
                    //do nothing
                } else {
                    if(B[i]>0) {
                        return B[i]+1; //if the first number is positive, we can say the number after it is the smallest possible integer
                    } else {
                        return 1
                    }
                }   
            } 
        }
        return max + 1;
    } else {
        return 1
    }

} else {
    return null;
}
}
Brahmi answered 22/12, 2017 at 19:14 Comment(0)
A
1
function solution(A){
    A.sort((a, b) => a-b);
    let int = 1;
    for(let i = 0; i < A.length; i++){

        if(A[i] > 0 && A[i] === int){
            int++;
        } else if (A[i] > 0 && A[i] > int){
            return int;
        }
    }
    return int;
}
Afghan answered 6/9, 2019 at 6:56 Comment(1)
Welcome to SO. You could improve your answer by providing a short description of the code. See help section (stackoverflow.com/help) for further guidance.Fcc
R
1

Task score: 100% Correctness: 100% Performance: 100 %

function solution(A) {
    // write your code in JavaScript (Node.js 8.9.4)

    let map = {};
    A.map(x => { map[x] = x; return x });

    let result = 1;
    while (true) {
        if (!map[result]) return result;
        result++;
    };
}
Rattail answered 17/9, 2019 at 9:48 Comment(0)
A
1

try the following,

let expecterNumber = 1;
        A.sort(function(a,b){
           return a - b; 
        });

        for (let i in A) {
            if (A[i] <= 0 || A[i] == A[i - 1]) continue 
            if (A[i] != expecterNumber) break

            expecterNumber++;
        }

        return expecterNumber;
}
Annunciator answered 16/12, 2019 at 2:18 Comment(0)
O
1

This working 100% with 100% performance

function solution(A) {
    ASorted = A.sort((a, b) => a - b );
    min = 1;
    for (let i = 0; i < ASorted.length; i++) {
        if (ASorted[i] === min) {
            min ++;
        }
    }
    return min;
}

Screenshot of result in Codility

Onagraceous answered 26/6, 2022 at 14:22 Comment(3)
Do you have any tests or data to back up your claims for the performance? Do you have any proof for anything to help bring value to your answer?Prominence
@Prominence Thanks for asking. I just ran it in codility and updated the answer with screenshot of result. Hope it helps.Onagraceous
GREAT solution! I'd never come up with it! very clever!Scevor
L
0

Try something like this:

// Array containing your numbers
var Arr = [1, 3, 6, 4, 1, 2];
solution(Arr);

function solution(Arr) {
    var len = Arr.length;
    var min = 100001;
    var num;

    // check if it is empty
    if (len == 0) {
        alert('Empty zero-indexed array given');
    }

    // find array's min number
    for(var i = 0; i < len; i++) {
        if (Arr[i] < min) {
            min = Arr[i];
        }
    }

    for (var i = 0; i < 100000; i++) {
        var x = Arr.indexOf(min);
        if (x == -1) {
            num = min;
            break;
        }
        min++;
    }

    alert('Min value in array is: ' + num);
}

Here is a working Jsfiddle demo.

Landsknecht answered 9/6, 2015 at 7:45 Comment(2)
Example test: [-1, -3] WRONG ANSWER (got -2 expected 1)Blasien
@Kamlesh my solution does not account for negative numbers. You can add an additional check like: if num < 0 always return 1, if you need to handle those as well.Landsknecht
L
0

I got 100% by this solution

function solution(A) {
    let  sortedOb={};
    let biggest=0;
    A.forEach(el=>{
        if(el>0)
        {
             sortedOb[el]=0;
              biggest =  el>biggest? el:biggest
        }
    });
    let arr = Object.keys(sortedOb).map(el=>+el);
    if(arr.length==0)
    return 1;
    for(let i = 1; i<=biggest; i ++){
        if(sortedOb[i] === undefined)
        return i
    }
    return biggest+1
}
Lucy answered 28/4, 2018 at 14:9 Comment(0)
G
0

you can try this: I used C#

static void Main(string[] args)
{

    int[] tempArray = new int[1000000];
    int[] givenArray = new int[] { 1, 2,3};   // or     { 1, 2,6,4,3}

    int results = myFunction(tempArray, givenArray);
    Console.WriteLine(results);
    Console.ReadKey();
}

private static int myFunction(int[] tempArray, int[] givenArray)
{
    int res = 0;

    for (int x = 1; x < tempArray.Length; x++)
    {

        if (!givenArray.Contains(x))
        {
            tempArray[x] = x;
        }
    }

    for (int y = 0; y < tempArray.Length; y++)
    {
        if (tempArray[y] > 0)
        {
            res = tempArray[y];
            break;
        }
    }
    return res;
}
Grin answered 15/6, 2018 at 13:44 Comment(0)
T
0

one more way to do it ::

  1. Sort the array and get lowest digit of array.

  2. Increment the lowest digit and check if it already exists.

  3. Return the lowest positive interger.

Got it !!

//var A =[1,3,4,10];
var i =0;
var solution = function(A) {
var min = 0;
var max = 0;
sortedArray = A.sort(function(a, b){return a-b});
min =sortedArray[0]; //min value
max =sortedArray[sortedArray.length-1];//max value in array

if(min < 0){
alert("min is 1"); //use return 1
}
else if(min > 0){
for(;i<sortedArray.length;i++){
x = checkifNoExists(min+i+1);//start checking the no. with min  
if(x){
    var lowest = min+i+1
    break;
}
}
alert(lowest);//use return lowest
}
}
checkifNoExists = function(no){//to check if no exists in array
for(var y = 0;y<sortedArray.length;y++){
if(sortedArray[y] == no){
var found = no
}
}
if(found == "" || found == undefined){
return true;
}else{
return false;
}   
}       
Tieratierce answered 5/10, 2018 at 11:19 Comment(0)
F
0

I tried to implement the JavaScript solution similar to the one I used in Java and came to realize that JavaScripts native Array.sort() was lacking performance...

I used a RadixSort implementation from @Blindman67 to sort the array and a simple loop and scored 100/100 @ O(N) or O(N * log(N)).

function solution(A) {
    A = radixSort(A);
    let min = 1;
    for (let i = 0; i <= A.length; ++i) {
      if (A[i] == min && min <= A.length) {
        min++;
      }
    }
    return min;
}

// RadixSort by: https://codereview.stackexchange.com/users/120556/blindman67
function radixSort(numbers) {
  function emptyBuckets() {          // empties buckets and adds contents back to workArray
    workArray.length = 0;
    for (i = 0; i < 10; i += 1) {   // could have used buckets forEach but this is quicker on average
      if (buckets[i].length > 0) {
        workArray.push(...buckets[i]);
        buckets[i].length = 0;
      }
    }
  }

  var i;                  // hoist declarations
  const results = [];     // array that holds the finnal sorted numbers
  const buckets = [[], [], [], [], [], [], [], [], [], []];  // buckets
  const workArray = [...numbers]; // copy the numbers
  var power = 0;                  // current digit as a power of ten
  var tenPow = 1;                 // ten to the power of power
  if (numbers.length <= 1) {        // if one or no items then dont sort
    return workArray;           // dont sort if there is no need.
  }
  // as numbers are sorted and moved to the result array the numbers
  while (workArray.length > 0) {
    for (i = 0; i < workArray.length; i += 1) { // for all numbers still being sorted
      if (workArray[i] < tenPow) {            // is the number samller than the current digit
        results.push(workArray[i]);         //Yes it is sorted then remove a put o nthe result array
      } else {
        // add to bucket. Use Math.floor and save complexity doing it in one statement line
        buckets[Math.floor(workArray[i] / tenPow) % 10].push(workArray[i]);
      }
    }
    power += 1;
    tenPow = Math.pow(10, power);
    emptyBuckets();
  }
  return results;
}
Furor answered 12/10, 2018 at 23:38 Comment(0)
M
0

My swift 3 solution (100/100)

public func solution(_ A : inout [Int]) -> Int {
  let set = Set(A)
  var value = 1
  while true {
    if !set.contains(value) {
      return value
    }
    value += 1
  }
}
Mcnelly answered 4/11, 2018 at 7:2 Comment(1)
what do you get then @Pierre.Vriens? I mean your correctness and performanceMcnelly
C
0

This was my solution that scored 66% on Task Score, 100% on Correctness and only 25% on Performance. Not the most performant solution but a working one none the less.

  1. First checks to make sure the highest number in the array is over 0, if not we end there with a result of 1
  2. Loop through numbers from 1 to highest number in array
  3. If that number does not have an index in the array, that is our result
  4. If all numbers from 1 to the highest in the array are present, the next highest number is the result
const test1 = [1, 3, 6, 4, 1, 2]; // Returns 5
const test2 = [1, 2, 3]; // Returns 4
const test3 = [-1, -3]; // Returns 1

function solution(A) {
  const lowestNum = Math.min.apply(Math, A);
  const highestNum = Math.max.apply(Math, A);

  // Check highestNum to make sure it's over 0
  if (highestNum > 0) {
    // Loop through all the numbers from 1 to the highes in the array
    for (var i = 1; i < highestNum; i++) {
      // If i does not have an index in the array, that's our solution
      if (Number(A.indexOf(i)) === -1) {
        return i;
      }
    }
    // If looped through array and all have an index, the next number is our answer
    return i + 1;
  } else {
  // If highestNum is not over 0, answer is 1
    return 1;
  }
}

solution(test1);

JS Fiddle demo

Charybdis answered 18/3, 2019 at 14:3 Comment(0)
S
0

Using Javascript, I did this in kind of an odd way but I got 100% on task score, correctness, and performance. However, I got an O(N) or O(N*log(N)). So, I'd like to lower that down.

function solution(A){
    // remove all negative and zero 
    let positiveArray = A.filter(x => x > 0);
    // sort all positive values
    let sortedArray = positiveArray.sort((x,y) => x-y);
    // return first that doesn't appear in order
    return sortedArray.reduce((prev, next) => {
        if(next === prev){
            prev++
        }
        return prev;
    },1);
}
Stephainestephan answered 9/5, 2019 at 0:0 Comment(0)
U
0

This is the solution that I tried during my test. Not that optimized but simple enough to read.

Idea:

  • Create a loop that will start from 1 as we are only looking for positive integer
  • For every iteration, check if current number is available in array.
  • If not available, break loop.

function solution(arr) {
  let i;
  let num = 1;
  for (i = 1; i <= arr.length; i++) {
    let exists = arr.some((n) => n === i);
    if (!exists) {
      num = i;
      break;
    }
  }
  return num;
}

console.log(solution([2, 3, 4, 5, 5]))

console.log(solution([2, 3, 4, 5, 5, 1]))

Solution 2 using hashmap:

This is a more performant and more advised solution.

  • Loop over array and create a hashmap of values. This will remove duplicates and also make lookup easy.
  • Also create a variable max and compute maximum value in array.
  • Now loop from 1 to max.
    • If value is not found, return index.
    • If all values are present, return max + 1

function solution(arr) {
  let max = 0;
  const hashMap = arr.reduce((map, num) => {
    map[num] = true;
    max = max > num ? max : num;
    return map
  }, {});
  
  for(let i = 1; i <= max; i++) {
    if (!hashMap[i]) return i
  }
  return max + 1;
}

console.log(solution([2, 3, 4, 5, 5]))

console.log(solution([2, 3, 4, 5, 5, 1]))
Uninhibited answered 17/9, 2019 at 10:1 Comment(0)
E
0
function solution(A) {
var swap = function(i, j) {
    var tmp = A[i];
    A[i] = A[j];
    A[j] = tmp;
};
for (let i = 0; i < A.length; i++) {
    while (0 < A[i] && A[i] - 1 < A.length
            && A[i] != i + 1
            && A[i] != A[A[i] - 1]) {
        swap(i,A[i] - 1);
    }
}
for (let i = 0; i < A.length; i++) {
      if (A[i] != i + 1) {
         return i + 1;
      }
   }
return A.length + 1;
}

Here is the final solution you are looking for

Erida answered 19/9, 2019 at 9:4 Comment(0)
S
0

Several great responses here. The most succinct and elegant IMO is https://stackoverflow.com/a/54079690 by @ben_flock.

However, just want to demonstrate a slightly more "efficient" option. I.E., it doesn't have to traverse the entire array after sorting. It short-circuits as soon as possible.

const solution = (values) => {
    values.sort( (a, b) => a-b ); // sort numerically, not lexically
    let smallest = 1; // default smallest positive integer
    values.some( value => 
        // ignore negative numbers
        // find the smallest integer not in the array
        value > 0 ? (value > smallest ? true : (smallest = value + 1) && false) : false
    );

    return smallest;
}

I used Array.prototype.sort(), Array.prototype.some(), and a ternary in this answer.

Solidus answered 25/10, 2019 at 19:19 Comment(0)
N
0

For JavaScript try following. Test score 100/100

Detected time complexity: O(N) or O(N * log(N))

function solution(A) {
     var missingInt = 1;

    A.sort((a, b) => { return a - b; });

    for (var i = 0; i < A.length; i++) {
        if (A[i] >= (missingInt + 1)) {
            break;
        }
        if (A[i] == missingInt) {
            missingInt++;
        }

    }

    return missingInt++;
}
Nautical answered 29/1, 2020 at 9:54 Comment(0)
A
0
function solution(A) {
    const dict = {};
    let startFrom = 1;
    for (let item of A) {
        dict[item] = true;            
        // keep startFrom greater than every number already in the dictionary
        while(dict[startFrom]) {
            startFrom++;
        }
    }
    return startFrom;
}
Ampoule answered 29/11, 2020 at 9:20 Comment(0)
D
0
  • First get max number of array.
  • Then iterate 1 to max number and check which number is not exist in array and return that number.
  • Else return max+1;
function solution(A) 
    {    
        var max = A.reduce(function(a, b) {
            return Math.max(a, b);
        });    
        for(var i = 1; i <= max; i++){
            if(!A.includes(i)){
                return i;
            }
        }
        if(i > max){
            return i;
        }
    }
Dodwell answered 20/1, 2021 at 18:22 Comment(0)
V
0
function solution(A) {
  // write your code in JavaScript (Node.js 8.9.4)
  const set = new Set();
  const N = A.length;
  let max = 0;

  A.forEach((ele) => {
    if (ele > 0) {

      set.add(ele);

      if (ele > max) {
        max = ele;
      }
    }
  });

  for (let i = 1; i < N + 1; i++) {
    if (!set.has(i)) {
      return i;
    }
  }

  return max + 1;
}
Vaunting answered 22/7, 2021 at 8:32 Comment(0)
W
0

Solution 1 A solution with complexity O(N Log N)

const solution = (A) => {
    A.sort((a, b) => a-b)
    let min = 1
    for(let item of A) {
        if(min < item) {
            return min
        }
        min = (item > 0 ? item + 1 : 1)
    }
    return min
}

console.log(solution([2, 1, -1, 1, 3, 5, 6]))
Warhol answered 19/9, 2021 at 9:29 Comment(0)
P
0

This is my solution 100% score without sorting

function solution(A) {
  const set = new Set(A);
  let missing = 1;

  while(true) {
    if (set.has(missing)) {
        missing++;
    } else {
        break;
    }
  }

  return missing;
}
Pacer answered 23/10, 2021 at 19:4 Comment(0)
P
0

As the requirement is to check if given input array is having positive numbers from 1, I will check if the numbers from 1 to given array length are present in the array or not.

Below is my solution

function solution(A) {
  
  let a;

  for(a = 1; a <= A.length; a++) {
     if(A.indexOf(a) === -1) return a;
  }
  return a;
}`
Polarimeter answered 22/12, 2021 at 15:30 Comment(0)
P
0

No need to use sort/set:

 function solution(A) {
    var obj = {};
    var inc = 1;
    A.forEach(item=>{
        obj[item]=item;
    });
    while(inc){
        if(!obj[inc]){
            return inc;
        }
        if(inc == 100000){
            return inc + 1;
        }
        inc++;
    }
    return inc;
}
Pest answered 20/1, 2022 at 16:30 Comment(0)
F
0

I've solved the problem and I got 100% score

function missingInteger(a) {
const map = {};

let largest;
for (let i = 0; i < a.length; i++) {
    const number = a[i];

    if (number <= 0) {
        continue;
    }

    if (!largest || number > largest) {
        largest = number;
    }

    map[number] = number;
}

if (!largest) {
    return 1;
}

let exists = true;
let i;
for (i = 1; i <= largest; i++) {
    if (map[i]) {
        continue;
    }

    exists = false;
    break;
}

if (!exists) {
    return i;
}

return largest + 1;
}
Fer answered 16/2, 2022 at 18:35 Comment(0)
G
0

This gave me a 100% score in Javascript.

let array = [...new Set(A)].sort((a,b)=>a-b);
let missing = 1;
array.forEach(ele=>{
    if (ele===missing){
        missing++;
    } else {
        return missing;
    }
});
return missing;

Its short and direct to the point.

Gayle answered 10/10, 2022 at 14:18 Comment(0)
A
0

I did it like this:

function solution(A) {
  let sortedA = A.sort((a,b) => a - b);
  let minVal = 1;

  sortedA.forEach(val => {
     if(minVal === val) {
       minVal++;
     }
  })

  return minVal;
}

console.log(solution([1, 3, 6, 4, 1, 2])) // will print 5
Attalanta answered 7/5 at 7:48 Comment(0)
E
0

Swift 5 with 100% solution:

public func solution(_ A : inout [Int]) -> Int {
    var occurs = Array(repeating: 0, count: A.count + 1)

    for i in A {
        if (1..<occurs.count).contains(i) {
            occurs[i] += 1
        }
    }

    for i in 1..<occurs.count {
        if occurs[i] == 0 {
            return i
        }
    }
    return A.count + 1
}
Eclosion answered 5/9 at 19:39 Comment(0)
T
-1

In JavaScript

1st Solution:

function solution(A) {
  for (i = 1; i < 1000000; i++) {
    if(!A.includes(i)) return i;
  }
}

2nd Solution:

function solution(A) {
  const set = new Set(A);
  let i = 1;
  while (set.has(i)) {
    i++;
  }
  return i;
}

3rd Solution:

function solution(A){
    A = A.sort(); 
    for (var i=0;i<A.length-1;i++){
        if(A[i]>0 && A[i+1]!=(A[i]+1))
            return (A[i]+1);
    }
}
Trench answered 15/4, 2020 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.