Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 187855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:53:53+00:00 2026-05-11T15:53:53+00:00

Given a list of integers, how can I best find an integer that is

  • 0

Given a list of integers, how can I best find an integer that is not in the list?

The list can potentially be very large, and the integers might be large (i.e. BigIntegers, not just 32-bit ints).

If it makes any difference, the list is ‘probably’ sorted, i.e. 99% of the time it will be sorted, but I cannot rely on always being sorted.

Edit –

To clarify, given the list {0, 1, 3, 4, 7}, examples of acceptable solutions would be -2, 2, 8 and 10012, but I would prefer to find the smallest, non-negative solution (i.e. 2) if there is an algorithm that can find it without needing to sort the entire list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T15:53:54+00:00Added an answer on May 11, 2026 at 3:53 pm

    One easy way would be to iterate the list to get the highest value n, then you know that n+1 is not in the list.

    Edit:

    A method to find the smallest positive unused number would be to start from zero and scan the list for that number, starting over and increase if you find the number. To make it more efficient, and to make use of the high probability of the list being sorted, you can move numbers that are smaller than the current to an unused part of the list.

    This method uses the beginning of the list as storage space for lower numbers, the startIndex variable keeps track of where the relevant numbers start:

    public static int GetSmallest(int[] items) {     int startIndex = 0;     int result = 0;     int i = 0;     while (i < items.Length) {         if (items[i] == result) {             result++;             i = startIndex;         } else {             if (items[i] < result) {                 if (i != startIndex) {                     int temp = items[startIndex];                     items[startIndex] = items[i];                     items[i] = temp;                 }                 startIndex++;             }             i++;         }     }     return result; } 

    I made a performance test where I created lists with 100000 random numbers from 0 to 19999, which makes the average lowest number around 150. On test runs (with 1000 test lists each), the method found the smallest number in unsorted lists by average in 8.2 ms., and in sorted lists by average in 0.32 ms.

    (I haven’t checked in what state the method leaves the list, as it may swap some items in it. It leaves the list containing the same items, at least, and as it moves smaller values down the list I think that it should actually become more sorted for each search.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there something existing in python that can convert an increasing list of integers
Here's a nice pitfall I just encountered. Consider a list of integers: List<Integer> list
I've got a System.Generic.Collections.List(Of MyCustomClass) type object. Given integer varaibles pagesize and pagenumber, how
Given an R list, I wish to find the index of a given list
Given a list of urls, I would like to check that each url: Returns
Surely there is a framework method that given an array of integers, strings etc
Suppose you are given a list L of n numbers and an integer k<n
Given a linked list of integers in random order, split it into two new
How can I store pairs of integers in a List? I know I could
Working in Python, given a list of N sets of integers from the range

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.