Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Add strings to an array if the array does not contain the string

Tags:

c#

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?

like image 473
RoflcoptrException Avatar asked Jun 08 '26 21:06

RoflcoptrException


1 Answers

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);
}
like image 50
p.s.w.g Avatar answered Jun 11 '26 10:06

p.s.w.g