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

  • Home
  • SEARCH
  • 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 42559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:21:25+00:00 2026-05-10T15:21:25+00:00

Suppose I have a collection (be it an array, generic List, or whatever is

  • 0

Suppose I have a collection (be it an array, generic List, or whatever is the fastest solution to this problem) of a certain class, let’s call it ClassFoo:

class ClassFoo {     public string word;     public float score;     //... etc ... }  

Assume there’s going to be like 50.000 items in the collection, all in memory. Now I want to obtain as fast as possible all the instances in the collection that obey a condition on its bar member, for example like this:

List<ClassFoo> result = new List<ClassFoo>(); foreach (ClassFoo cf in collection) {     if (cf.word.StartsWith(query) || cf.word.EndsWith(query))         result.Add(cf); } 

How do I get the results as fast as possible? Should I consider some advanced indexing techniques and datastructures?

The application domain for this problem is an autocompleter, that gets a query and gives a collection of suggestions as a result. Assume that the condition doesn’t get any more complex than this. Assume also that there’s going to be a lot of searches.

  • 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. 2026-05-10T15:21:26+00:00Added an answer on May 10, 2026 at 3:21 pm

    With the constraint that the condition clause can be ‘anything’, then you’re limited to scanning the entire list and applying the condition.

    If there are limitations on the condition clause, then you can look at organizing the data to more efficiently handle the queries.

    For example, the code sample with the ‘byFirstLetter’ dictionary doesn’t help at all with an ‘endsWith’ query.

    So, it really comes down to what queries you want to do against that data.

    In Databases, this problem is the burden of the ‘query optimizer’. In a typical database, if you have a database with no indexes, obviously every query is going to be a table scan. As you add indexes to the table, the optimizer can use that data to make more sophisticated query plans to better get to the data. That’s essentially the problem you’re describing.

    Once you have a more concrete subset of the types of queries then you can make a better decision as to what structure is best. Also, you need to consider the amount of data. If you have a list of 10 elements each less than 100 byte, a scan of everything may well be the fastest thing you can do since you have such a small amount of data. Obviously that doesn’t scale to a 1M elements, but even clever access techniques carry a cost in setup, maintenance (like index maintenance), and memory.

    EDIT, based on the comment

    If it’s an auto completer, if the data is static, then sort it and use a binary search. You’re really not going to get faster than that.

    If the data is dynamic, then store it in a balanced tree, and search that. That’s effectively a binary search, and it lets you keep add the data randomly.

    Anything else is some specialization on these concepts.

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

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Try escaping the .. with something like: Uri target =… May 11, 2026 at 6:26 am
  • added an answer As Kobi wrote, you can find the required information in… May 11, 2026 at 6:26 am
  • added an answer I think Eclipse / EPIC has "some" Perl refactoring tools...… May 11, 2026 at 6:26 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.