I have a huge collection of strings. I will find out all the strings which starts with the given character more frequently. What would be a best collection to do this. I will initialize the collection in sorted order.
Thanks
I have a huge collection of strings. I will find out all the strings which starts with the given character more frequently. What would be a best collection to do this. I will initialize the collection in sorted order.
Thanks
If you want a map from a character to all strings starting with that character, you might find ILookup<TKey, TElement>
suitable. It's very similar to a Dictionary<TKey, TValue>
, with two main differences:
Instead of a 1:1 mapping, it performs a 1:n mapping (i.e. there can be more than one value per key).
You cannot instantiate (new
) nor populate it (.Add(…)
) yourself; instead, you let .NET derive a fully populated instance from another collection by calling .ToLookup(…)
on the latter.
Here's an example how to build such a 1:n map:
using System.Collections.Generic; // for List<T>
using System.Linq; // for ILookup<TKey, TValue> and .ToLookup(…)
// This represents the source of your strings. It doesn't have to be sorted:
var strings = new List<string>() { "Foo", "Bar", "Baz", "Quux", … };
// This is how you would build a 1:n lookup table mapping from first characters
// to all strings starting with that character. Empty strings are excluded:
ILookup<char, string> stringsByFirstCharacter =
strings.Where(str => !string.IsNullOrEmpty(str)) // exclude empty strings
.ToLookup(str => str[0]); // key := first character
// This is how you would look up all strings starting with B.
// The output will be Bar and Baz:
foreach (string str in stringsByFirstCharacter['B'])
{
Console.WriteLine(str);
}
P.S.: The above hyperlink for
ILookup<…>
(the interface) refers you to the help page forLookup<…>
(the implementation class). This is on purpose, as I find the documentation for the class easier to read. I would however recommend to use the interface in your code.
If you need to search regularly with a huge collection of strings, then use a Hash table. Remember to distribute the table evenly to speed up the look-up operation.
Well so you need to create an index on function from string.
For this Id suggest using
Dictionary<string,List<string>>
data structure.
ToLookup isn't so good cause it limits your ability to maniuplate the data structure.