Question

I have a list of strings:

 var list = new List<string>();
 list.Add("CAT");
 list.Add("DOG");

var listofItems = new List<string>();
 listofItems .Add("CATS ARE GOOD");
 listofItems .Add("DOGS ARE NICE");
 listofItems .Add("BIRD");
 listofItems .Add("CATAPULT");
 listofItems .Add("DOGGY");

and now i want a function like this:

 listofItems.Where(r=> list.Contains(r));

but instead of Contains, i want it to do a starts with check so 4 out of the 5 items would be returned (BIRD would NOT).

What is the fastest way to do that?

Was it helpful?

Solution

You can use StartsWith inside of an Any

listofItems.Where(item=>list.Any(startsWithWord=>item.StartsWith(startsWithWord)))

You can visualize this as a double for loop, with the second for breaking out as soon as it hits a true case

var filteredList = new List<String>();
foreach(var item in listOfItems)
{
    foreach(var startsWithWord in list)
    {
        if(item.StartsWith(startsWithWord))
        {
            filteredList.Add(item)
            break;
        }
    }
}
return filteredList;

OTHER TIPS

The fastest way would be usage of another data structure, for example Trie. Basic C# implementation can be found here: https://github.com/kpol/trie

This should get you what you need in a more simplified format:

 var result = listofItems.Select(n =>
 {
      bool res = list.Any(v => n.StartsWith(v));
      return res 
               ? n 
               : string.Empty;
 }).Where(b => !b.Equals(string.Empty));

The Trie data structure is what you need. Take a look at this more mature library: TrieNet

using Gma.DataStructures.StringSearch;

...

var trie = new SuffixTrie<int>(3);

trie.Add("hello", 1);
trie.Add("world", 2);
trie.Add("hell", 3);

var result = trie.Retrieve("hel");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top