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 7742653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:19:46+00:00 2026-06-01T09:19:46+00:00

I’m trying to find a reasonably efficient way of getting any single integer not

  • 0

I’m trying to find a reasonably efficient way of getting any single integer not in a set. So for example, if I have numbers={1, 4, 9}, then a valid result would be 3. I can do it like this:

n = random.randint(-100, 100)
if n not in numbers:
    return n

But I don’t want to be constrained to an arbitrary range (e.g. -100 -> 100) since I have no idea how large the set will be. The other option would be to iterate over every integer, but that would be terribly inefficient.

Does anyone have any suggestions about a better way of doing this?

Edit: Because of the number of questions about exactly what I’m trying to do, I’m updating this question to explain some of the background.

What I am actually trying to achieve is a mapping something like this: {a: 1, b: 2, c: 1} where a, b and c are object instances. The values in this unique for a group, so I can tell that a and c are in group 1 and b is in group 2. The actual number is irrelevant, it’s just a unique key for the group and doesn’t relate to anything outside this structure. The actual structure is a database table whith two fields, both of which are indexed so that I can quickly find out, for example, what else is in the same group as a.

Now what I need the unique number for, is when I want to add a group. This doesn’t happen very often so it doesn’t have to be incredibly efficient, but since the amount of data can get quite large I need to keep the number of iterations down. I realise that there are a few simple ways of doing this to with acceptable limitations, e.g. using randint with a large range (e.g. 1e6), or possibly even using a database function. But since I’ve been thinking about this it’s become a matter of interest to find a neat solution for populating the values without hardcoded limits. Obviously memory limitations (e.g. the max size of an integer) still apply.

  • 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. Editorial Team
    Editorial Team
    2026-06-01T09:19:47+00:00Added an answer on June 1, 2026 at 9:19 am

    any single integer not in a set

    But there’s an infinite number of integer values, so there’s an infinite number of integer values not in a finite set. Unless you’re talking about integer within a finite range (e.g. 16 bits).

    The most efficient solution will depend on the how complete the set of integers is – if it’s sparse then picking a number at random is likely to return one not in the set more often. If there are few gaps, then an optimised search will be more efficient. Both of these depend on having a sorted list of the set.

    Looking at the search method, it’s possible to speed up the search by partitioning the data: calculate the mean M of the lowest and highest index of numbers in the list, if dataset[M]<(M-dataset[0]) then there’s a gap, otherwise check if dataset[last]<(dataset[0]+last) in which case there’s a gap in the second half, repeat the process for the half of the data which has a gap.

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.