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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:08:25+00:00 2026-05-31T22:08:25+00:00

Wondering if I can speed up the search. I need to build a functionality

  • 0

Wondering if I can speed up the search.
I need to build a functionality that has to be used by many UI screens
The one I have got works but I need to make sure I am implementing a fast algoritim if you like

It’s like an incremental search.

User types a word to search for eg

        const string searchFor = "Guinea";
        const char nextLetter = ' '

It looks in the list and returns 2 records

     "Guinea and Guinea Bissau "

User types a word to search for eg

        const string searchFor = "Gu";
        const char nextLetter = 'i'

    returns
    3 results.

This is the function but I would like to speed it up.
Is there a pattern for this kind of search?

class Program
{
    static void Main()
    {
        //Find all countries that begin with string + a possible letter added to it
        //const string searchFor = "Guinea";
        //const char nextLetter = ' ';  //returns 2 results

        const string searchFor = "Gu";
        const char nextLetter = 'i';
        List<string> result = FindPossibleMatches(searchFor, nextLetter);
        result.ForEach(x=>Console.WriteLine(x)); //returns 3 results



        Console.Read();
    }
    /// <summary>
    /// Find all possible matches 
    /// </summary>
    /// <param name="searchFor">string to search for</param>
    /// <param name="nextLetter">pretend user as just typed  a letter</param>
    /// <returns></returns>
    public static List<string> FindPossibleMatches (string searchFor, char nextLetter)
    {
        var hashedCountryList = new HashSet<string>(CountriesList());
        var result=new List<string>();

        IEnumerable<string> tempCountryList = hashedCountryList.Where(x => x.StartsWith(searchFor));

        foreach (string item in tempCountryList)
        {
            string tempSearchItem;
            if (nextLetter == ' ')
            {
                tempSearchItem = searchFor;
            }
            else
            {
                tempSearchItem = searchFor + nextLetter;
            }
            if(item.StartsWith(tempSearchItem))
            {
                result.Add(item);
            }

        }
        return result;
    }



    /// <summary>
    /// Returns list of countries.
    /// </summary>
    public static string[] CountriesList()
    {
        return new[]
            {
                "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
                "Angola", "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina",
                "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
                "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
                "Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
                "Bolivia", "Bosnia Hercegovina", "Botswana", "Bouvet Island", "Brazil",
                "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Byelorussian  SSR",
                "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",
                "Central African Republic", "Chad", "Chile", "China", "Christmas Island",
                "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Cook Islands",
                "Costa Rica", "Cote D'Ivoire", "Croatia", "Cuba", "Cyprus",
                "Czech Republic", "Czechoslovakia", "Denmark", "Djibouti", "Dominica",
                "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador",
                "England", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
                "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France",
                "Gabon", "Gambia", "Georgia", "Germany", "Ghana",
                "Gibraltar", "Great Britain", "Greece", "Greenland", "Grenada",
                "Guadeloupe", "Guam", "Guatemela", "Guernsey", "Guiana",
                "Guinea", "Guinea Bissau", "Guyana", "Haiti", "Heard Islands",
                "Honduras", "Hong Kong", "Hungary", "Iceland", "India",
                "Indonesia", "Iran", "Iraq", "Ireland", "Isle Of Man",
                "Israel", "Italy", "Jamaica", "Japan", "Jersey",
                "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, South",
                "Korea, North", "Kuwait", "Kyrgyzstan", "Lao People's Dem. Rep.", "Latvia",
                "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein",
                "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar",
                "Malawi", "Malaysia", "Maldives", "Mali", "Malta",
                "Mariana Islands", "Marshall Islands", "Martinique", "Mauritania", "Mauritius",
                "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco",
                "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar",
                "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
                "Neutral Zone", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
                "Nigeria", "Niue", "Norfolk Island", "Northern Ireland", "Norway",
                "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea",
                "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland",
                "Polynesia", "Portugal", "Puerto Rico", "Qatar", "Reunion",
                "Romania", "Russian Federation", "Rwanda", "Saint Helena", "Saint Kitts",
                "Saint Lucia", "Saint Pierre", "Saint Vincent", "Samoa", "San Marino",
                "Sao Tome and Principe", "Saudi Arabia", "Scotland", "Senegal", "Seychelles",
                "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
                "Somalia", "South Africa", "South Georgia", "Spain", "Sri Lanka",
                "Sudan", "Suriname", "Svalbard", "Swaziland", "Sweden",
                "Switzerland", "Syrian Arab Republic", "Taiwan", "Tajikista", "Tanzania",
                "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago",
                "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu",
                "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States",
                "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela",
                "Vietnam", "Virgin Islands", "Wales", "Western Sahara", "Yemen",
                "Yugoslavia", "Zaire", "Zambia", "Zimbabwe"
            };
    }
}

}

Any suggestions?
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-31T22:08:27+00:00Added an answer on May 31, 2026 at 10:08 pm

    The Data structure which is used for prefix searching is TRIE. You can read more about the data structure over here:
    http://en.wikipedia.org/wiki/Trie

    You can found couple of c# implementations of trie in following links:
    http://www.kerrywong.com/2006/04/01/implementing-a-trie-in-c/

    https://codereview.stackexchange.com/questions/2195/is-this-a-reasonable-trie-implementation

    Hope this helps in speed.

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

Sidebar

Related Questions

I was wondering how can I programmatically copy all the discussion items from one
I'm new to JQuery and was wondering how can I have Changes saved! displayed
I was wondering if anyone had some resources that describe the binary protocol used
I have a site that has to crawl different sites to aggregate information. When
can some one provided some suggestions of constructing MySQL querys that are both fast
I have been trying to find a collision avoidance example that I can adapt
I have a database full of recipes, one recipe per row. I need to
I am wondering if there are high speed min and max function that works
i'm wondering how can i sync the speed (obviously, to the less fast) of
I was wondering how can I set a timeout on a socket_read call? The

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.