smallest positive integer not in array java

If the expected running time should be linear, you can't use a TreeSet, which sorts the input and therefore requires O(NlogN). Therefore you should use a HashSet, which requires O(N) time to add N elements.

Besides, you don't need 4 loops. It's sufficient to add all the positive input elements to a HashSet (first loop) and then find the first positive integer not in that Set (second loop).

int N = A.length;
Set<Integer> set = new HashSet<>();
for (int a : A) {
    if (a > 0) {
        set.add(a);
    }
}
for (int i = 1; i <= N + 1; i++) {
    if (!set.contains(i)) {
        return i;
    }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
return smallest positive integer not in array find the smallest positive integer in an array that is not in the array Smallest Positive missing number in java smallest positive integer in array smallest non negative integer in an array java function that takes in array of integers and returns smallest value smallest integet which is not in the array of given integers find the smallest number inside of array java smallest positive integer that does not occur in array smallest positive number which is not occur in array java find the smallest positive integer in a array java java find the smallest positive integer that does not occur in a given sewuence java find smallest number not in array given an array of integers find the smallest positive integer that doesn't appear how to find smallest positive integer in array java find smallest integere in array jva find smallest positive integer in array java smallest positive integer in java find the smallest positive number missing from an unsorted array in java returns the smallest positive integer (greater than 0) that does not occur in a. java smallest positive integer not in array java program to find the smallest positive number missing from an unsorted array return the smallest positive integer that does not occur in array java function to return smallest positive integer smallest number in an array java Find the smallest positive integer that does not occur in a given sequence in java function that return smallest positive integer not in array java smallest number in array java smallest number from an array java find smallest number in an array of n values in java write a function that given an array a of n integers returns the smallest positive integer java that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. java find smallest positive integer not in array smallest postive integer not in array java java find smallest int in array given an array of integers return the smallest positive integer not in it smallest positive integer not in array java
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source