Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
Find lowest positive integer that does not appear in array

I am trying to solve a leetcode type problem that is a practice problem that came with an upcoming code test I need to do for a job and I am having trouble with it. Can anyone help me understand whats going wrong?

I am essentially looking for the brute force option as I dont know algos/DS.

                                                       PROBLEM:

Write a function:

function solution(A);

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.

Given A = [1, 2, 3], the function should return 4.

Given A = [−1, −3], the function should return 1.

Write an efficient algorithm for the following assumptions:

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

                            HERE IS MY SOLUTION: 

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

The below snippet isnt working like I expect it to. lowestNumber isnt being increased and also the loop is exiting here I believe.

if(lowestNumber == newArray[i]) {
                lowestNumber = lowestNumber + 1

Thanks for your help!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think your > should be <, and the = in if(i = newArray.length - 1) should be ===.

And lowestNumber > newArray[0] will always be true if the array contains a negative number, so 1 will be returned.

Your effort seems careless, so you are going to have to up your game for the interview.

const integers = [5, -345, 562456, 95345, 4, 232, 1, 2, 3, 7, -457];

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

console.log(solution(integers));

about 4 years ago · Juan Pablo Isaza Report

0

You can do this in O(N) using a Map():

  • First set every number in the array.
  • Then starting from 1 look for and return the missing number in the sequence.

function solution(arr) {
  const seen = new Map();

  for (let i = 0; i < arr.length; i++) {
    seen.set(arr[i]);
  }

  for (let i = 1; i <= arr.length + 1; i++) {
    if (!seen.has(i)) return i;
  }

  return 1;
}

console.log(solution([1, 3, 6, 4, 1, 2])); //-> 5
console.log(solution([1, 2, 3]));          //-> 4
console.log(solution([-1, -3]));           //-> 1

about 4 years ago · Juan Pablo Isaza Report

0

The fastest solution

function solution(A) {
  // write your code in JavaScript (Node.js 8.9.4)
  if (!A) return 1;
  A.sort();
  if (A[A.length - 1] < 1) return 1;
  const setA = new Set(A);
  let length = setA.size;
  for (let i = 1; i <= length; i++) {
    if (!setA.has(i)) {
      return i;
    }
  }

  return length + 1;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!