Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I return the smallest missing positive int in array in js

how can I take the samllest missing positive integer from array of integers in js (I didn’t fiund any answer for my question, all I found was in other languages)

example

[-2, 6, 4, 5, 7, -1, 1, 3, 6, -2, 9, 10, 2, 2]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

good question

let nums = [-2, 6, 4, 5, 7, -1, 1, 3, 6, -2, 9, 10, 2, 2];

console.log(smallestMissing(nums));

function smallestMissing(nums) {
    let n = 1;
    while (nums.includes(n)) n++;
    return n
}

EDIT:
Can you please try to tell me what’s wrong with my code without downvoting?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading