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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:57:33+00:00 2026-05-16T04:57:33+00:00

Say there’s a list. Each item in the list has a unique id. List

  • 0

Say there’s a list. Each item in the list has a unique id.

List [5, 2, 4, 3, 1]

When I remove an item from this list, the unique id from the item goes with it.

List [5, 2, 3, 1]

Now say I want to add another item to the list, and give it the least lowest unique id.

What’s the easiest way to get the lowest unique id when adding a new item to the list?

Here’s the restriction though: I’d prefer it if I didn’t reassign the unique id of another item when deleting an item.

I realise it would be easy to find the unique id if I reassigned unique id 5 to unique id 4 when I deleted 4. Then I could get the length of the list (5) and creating the new item with the unique id with that number.

So is there another way, that doesn’t involve iterating through the entire list?

EDIT:

Language is java, but I suppose I’m looking for a generic algorithm.

  • 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-05-16T04:57:33+00:00Added an answer on May 16, 2026 at 4:57 am

    An easy fast way is to just put your deleted ids in a priority queue, and just pick the next id from there when you insert new ones (or use size() + 1 of the first list as id when the queue is empty). This would however require another list.

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

Sidebar

Related Questions

Say there is a website with 100,000 users each has up to 1000 unique
Say there is a site foo.com which loads JavaScript from site bar.com . Now,
Say there is a div that has content and a youtube link. I want
Say there are several files modified and I just want one files committed each
Say there is a list of document IDs and i want to retrieve the
Say there are 3 models: A, B, and C. Each of these models has
Say there are three classes: A , B & C . I want each
Say there is an object A A has a strong reference to B B
Say there are navigations, nav1, nav2, nav3 and many subnavs within each of them.
Say there is such table: mysql> SELECT * FROM tags; +---------+--------+ | post_id |

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.