0)
Split
the strings on comma delimiters.
1) Add each array's element (from the
Split
statement) to a
HashSet
object (a
HashSet will not allow duplicate element values).
2) Output the contents of the
HashSet
object.
string[] parts1 = string1.Split(',', false);
string[] parts2 = string2.Split(',', false);
HashSet<string> hash = new HashSet<string>();
foreach(string part in parts1)
{
hash.Add(part);
}
foreach(string part in parts2)
{
hash.Add(part);
}
foreach(string part in hash)
{
Console.WriteLine(part);
}
Console.ReadKey();
The benefit of using a
HashSet
object is that it will not throw an exception if you try to add a duplicate value. Instead, it simply won't add it. A
HashSet
is not guaranteed to be in any order, so I leave it as an exercise for the programmer to return the values in the desired sequence.