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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:59:29+00:00 2026-06-15T04:59:29+00:00

One wants to store objects, allowing them to be retrieved via a numerical key.

  • 0

One wants to store objects, allowing them to be retrieved via a numerical key. These keys can range from 0 to an arbitrary size (~100K, for instance), but not every natural number in the range has a corresponding object.

One might have the following:

structure[0] => some_obj_a
structure[3] => some_obj_b
structure[7] => some_obj_c
...
structure[100103] => some_obj_z

But all other keys (1, 2, 4, 5, 6, …) do not have an associated object. The numerical keys are used for retrieval, such that an “ID” is provided to return an object associated to that ID:

ID = get_input_id
my_obj = structure[ID]

What is the most efficient data structure for this scenario in Ruby? And for what reasons? (So far, I can see it being a hash or an array.)

I define “efficient” in terms of:

  • Least memory used
  • Fastest lookup times
  • Fastest entry creation/updates (at arbitrary keys)

An initialization for this structure might be

hsh = Hash.new # or Array.new
hsh[0] = {:id => 0, :var => "a", :count => 45}
hsh[3] = {:id => 3, :var => "k", :count => 32}
hsh[7] = {:id => 7, :var => "e", :count => 2}
  • 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-15T04:59:30+00:00Added an answer on June 15, 2026 at 4:59 am

    You’ve essentially described a sparse array or a hash.

    Hashes are fast and only use the memory they have to use. They are also memory efficient. There is no “magic” data structure for this that’ll be any faster. Use a hash.

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

Sidebar

Related Questions

There are cases when one needs a memory efficient to store lots of objects.
I have a number of one dimension arrays of Object[] (these objects are primitive
In one of our application we want to store different type of objects in
Box2d objects have a void* m_userData field where the user can store data. Typically
I want to store some objects where there is exactly one Bar for each
I want to store a data structure thats a table with one column Strings
I want to store JSP session data in a custom store (the one I
Google App Engine - When I want to store a byte array as one
I have a table which i want to store all the outputs into one
I'm lazy! And I just want to store one single string value. That's all!

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.