I have a lot of string arrays. From all these string arrays, I want to create an array of unique strings. At the moment I do it like this:
string[] strings = {};
while(running)
{
newStringArrayToAdd[] = GetStrings();
strings = strings.Concat(newStringArrayToAdd).ToArray();
}
uniqueStrings = strings.Distinct.ToArray();
This works but it is very very slow since I have to keep the strings variable in memory which gets very huge. Therefore I'm looking for a way to check on the fly if a string is in uniqueStrings and if not add it immediately. How can I do that?
Consider using a HashSet<string> instead of an array. It will do nothing if the string already exists in the set:
HashSet<string> strings = new HashSet<string>();
strings.Add("foo");
strings.Add("foo");
strings.Count // 1
The UnionWith method will be very useful in your example code:
HashSet<string> strings = new HashSet<string>();
while(running)
{
string[] newStringArrayToAdd = GetStrings();
strings.UnionWith(newStringArrayToAdd);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With