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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:54:17+00:00 2026-05-28T19:54:17+00:00

This is a question about returning efficiently strings and chars from a string array

  • 0

This is a question about returning efficiently strings and chars from a string array where:

  1. The string in the string array starts with the user input supplied
  2. The next letter of those strings as a collection of chars.

The idea is that when the user types a letter, the potential responses are displayed along with their next letters. Therefore response time is important, hence a performant algorithm is required.

E.g. If the string array contained:

string[] stringArray = new string[] { "Moose", "Mouse", "Moorhen", "Leopard", "Aardvark" };

If the user types in “Mo”, then “Moose”, “Mouse” and “Moorhen” should be returned along with chars “o” and “u” for the potential next letters.

This felt like a job for LINQ, so my current implementation as a static method is (I store the output to a Suggestions object which just has properties for the 2 returned lists):

public static Suggestions 
GetSuggestions
    (String userInput, 
    String[] stringArray)
{
    // Get all possible strings based on the user input. This will always contain
    // values which are the same length or longer than the user input.
    IEnumerable<string> possibleStrings = stringArray.Where(x => x.StartsWith(userInput));
    IEnumerable<char> nextLetterChars = null;

    // If we have possible strings and we have some input, get the next letter(s)
    if (possibleStrings.Any() &&
        !string.IsNullOrEmpty(userInput))
    {
        // the user input contains chars, so lets find the possible next letters.
        nextLetterChars =
            possibleStrings.Select<string, char>
            (x =>
            {
                // The input is the same as the possible string so return an empty char.
                if (x == userInput)
                {
                    return '\0';
                }
                else
                {
                    // Remove the user input from the start of the possible string, then get
                    // the next character.
                    return x.Substring(userInput.Length, x.Length - userInput.Length)[0];
                }
            });
    } // End if

I implemented a second version which actually stored all typing combinations to a list of dictionaries; one for each word, with key on combination and value as the actual animal required, e.g.:

  • Dictionary 1:
  • Keys Value
  • “M” “Moose”
  • “MO “Moose”

Etc.

  • Dictionary 2:
  • Keys Value
  • “M” “Mouse”
  • “MO” “Mouse”

Etc.

Since dictionary access has an O(1) retrieval time – I thought perhaps this would be a better approach.

So for loading the dictionaries at start up:

    List<Dictionary<string, string>> animalCombinations = new List<Dictionary<string, string>>();
    foreach (string animal in stringArray)
    {
        Dictionary<string, string> animalCombination = new Dictionary<string, string>();
        string accumulatedAnimalString = string.Empty;
        foreach (char character in animal)
        {
            accumulatedAnimalString += character;
            animalCombination[accumulatedAnimalString] = animal;
        }

        animalCombinations.Add(animalCombination);
    }

And then at runtime to get possible strings:

    // Select value entries from the list of dictionaries which contain
    // keys which match the user input and flatten into one list.
    IEnumerable<string> possibleStrings = 
        animalCombinations.SelectMany
            (animalCombination => 
                {
                    return animalCombination.Values.Where(x =>
                        animalCombination.ContainsKey(userInput));
                });

So questions are:

  1. Which approach is better?
  2. Is there a better approach to this which has better performance?
  3. Are LINQ expressions expensive to process?

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-28T19:54:18+00:00Added an answer on May 28, 2026 at 7:54 pm

    Which approach is better?

    Probably the dictionary approach, but you’ll have to profile to find out.

    Is there a better approach to this which has better performance?

    Use a prefix tree.

    Are LINQ expressions expensive to process?

    Written correctly, they add very little overhead to imperative versions of the same code. Since they are easier to read and maintain and write, they are usually the way to go.

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

Sidebar

Related Questions

I have a question about returning results from a pthread executed function. Related code:
This question about Timers for windows services got me thinking: Say I have (and
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I just came across this question about initializing local variables. Many of the answers
I have seen this question about deploying to WebSphere using the WAS ant tasks.
Follow up to this question about GNU make : I've got a directory, flac
I have a question about this question . I posted a reply there but
This is sort of the Java analogue of this question about C# . Suppose

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.