C#: Add strings to an array if the array does not contain the string
c#
Solution
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);
}
Problem
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?