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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:25:05+00:00 2026-05-23T12:25:05+00:00

I often* find myself in need of a data structure which has the following

  • 0

I often* find myself in need of a data structure which has the following properties:

  • can be initialized with an array of n objects in O(n).
  • one can obtain a random element in O(1), after this operation the picked
    element is removed from the structure.
    (without replacement)
  • one can undo p ‘picking without replacement’ operations in O(p)
  • one can remove a specific object (eg by id) from the structure in O(log(n))
  • one can obtain an array of the objects currently in the structure in
    O(n).

the complexity (or even possibility) of other actions (eg insert) does not matter. Besides the complexity it should also be efficient for small numbers of n.

Can anyone give me guidelines on implementing such a structure? I currently implemented a structure having all above properties, except the picking of the element takes O(d) with d the number of past picks (since I explicitly check whether it is ‘not yet picked’). I can figure out structures allowing picking in O(1), but these have higher complexities on at least one of the other operations.

BTW:
note that O(1) above implies that the complexity is independent from #earlier picked elements and independent from total #elements.

*in monte carlo algorithms (iterative picks of p random elements from a ‘set’ of n elements).

  • 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-23T12:25:06+00:00Added an answer on May 23, 2026 at 12:25 pm

    Ok, same answer as 0verbose with a simple fix to get the O(1) random lookup. Create an array which stores the same n objects. Now, in the HashMap, store the pairs . For example, say your Objects (strings for simplicity) are:

    {"abc" , "def", "ghi"}
    

    Create an

    List<String> array = ArrayList<String>("abc","def","ghi")
    

    Create a HashMap map with the following values:

    for (int i = 0; i < array.size(); i++) 
    {
        map.put(array[i],i);
    }
    

    O(1) random lookup is easily achieved by picking any index in the array. The only complication that arises is when you delete an object. For that, do:

    1. Find object in map. Get its array index. Lets call this index i (map.get(i)) – O(1)

    2. Swap array[i] with array[size of array – 1] (the last element in the array). Reduce the size of the array by 1 (since there is one less number now) – O(1)

    3. Update the index of the new object in position i of the array in map (map.put(array[i], i)) – O(1)

    I apologize for the mix of java and cpp notation, hope this helps

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

Sidebar

Related Questions

I seem to often find myself wanting to store data of more than one
Often I find myself filling ASP.NET repeaters with items that need the CSS class
I often find myself with a list of disconnected Linq2Sql objects or keys that
I find myself often writing statements equivalent to: deleted_at = Time.at(data[:deleted_at]) if !data[:deleted_at].nil? i'd
In Emacs, I often find myself in a situation where I need to jump
I find myself writing this class often in my python code when I need
I often find myself using Integers to represent values in different spaces. For example...
I often find myself confused with how the terms 'arguments' and 'parameters' are used.
I often find myself writing a property that is evaluated lazily. Something like: if
While creating classes in Java I often find myself creating instance-level collections that I

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.