JSFiddle - React, Tailwind, and code Playground

by Abhishek Kumar

JavaScript

function solution(A) {
  // create a set of the positive elements in A
  let positives = new Set(A.filter(x => x > 0));
  // start from 1 and check if it is in the set
  let answer = 1;
  while (positives.has(answer)) {
    // if yes, increment the answer and repeat
    answer++;
  }
  // if no, return the answer as the smallest positive integer
  return answer;
}

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