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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:34:14+00:00 2026-05-20T04:34:14+00:00

The select method in Ruby is simple and straight forward. It will select elements

  • 0

The select method in Ruby is simple and straight forward. It will select elements in an array matching a specific criteria.

For example,

>> x = [4,5,7,89,4,5,3,6,8,9,4,45,56,23,2,7,3,5,4,224,234,565,546,345,23,234,234,234,23466,25,54]
x = [4,5,7,89,4,5,3,6,8,9,4,45,56,23,2,7,3,5,4,224,234,565,546,345,23,234,234,234,23466,25,54]
=> [4, 5, 7, 89, 4, 5, 3, 6, 8, 9, 4, 45, 56, 23, 2, 7, 3, 5, 4, 224, 234, 565, 546, 345, 23, 234, 234, 234, 23466, 25, 54]
>> y = x.select{|m| m>20 && m<200}
y = x.select{|m| m>20 && m<200}
=> [89, 45, 56, 23, 23, 25, 54]

One problem about this is obviously is the time penalty. Select has to go through all the values in that array and does a sequential check which would result this run in O(n) time. Is there a better alternative to select which does it in a lesser time. Space is not an issue for me.

I am talking on cases where the same select is being used repetitively. If I am going to use the same select conditions 1000 times in a loop for an array of size n, then I will have to do the operation like 1000 * n times. Whereas if its optimized for space, I would only be doing 1000 * 1 times.

Thanks.

  • 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-20T04:34:14+00:00Added an answer on May 20, 2026 at 4:34 am

    I’m not aware of a way to perform an operation on every element in an array in better than linear time — the idea sounds self-contradictory. You could memoize the result if you need to do the same calculation on the same array more than once to trade space for constant-time performance on subsequent runs, but I think O(n) is as good as it gets otherwise.

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

Sidebar

Related Questions

Is there an easy and straight-forward method to select elements based on their data
Ruby has a select method that takes an array and returns a subarray consisting
hopefully a pretty simple question this time. I have a Select method in a
I am using the .eq() method to select a specific child element of a
Ruby's Enumerable has a select which can select certain items from an array: parent=@sections.select
When using the select.select() method in Python, is there any way to use input
I have a datatable on which I am applying Select() method. After that I
I have a dropdown box where the user can select a method to check
I was using the Add class method $(select[name=' + ABC+ i + ']).addClass('addBorder'); This
In a .Net web application I use the public DataRow[] Select(string filterExpression) method in

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.