C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part 1: We create an array that contains several duplicated strings: the string "cat" is repeated 3 times.
String LiteralPart 2: We use the HashSet constructor, which takes the union of elements. It internally calls UnionWith to eliminate duplicates.
Part 3: We invoke ToArray to convert the HashSet into a new array, which may be easier to use elsewhere.
ToArrayPart 4: The program displays string arrays onto the console or as single strings using the string.Join static method.
StaticJoinC# program that uses HashSet on duplicates
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Part 1: input array that contains three duplicate strings.
string[] array1 =
{
"cat",
"dog",
"cat",
"leopard",
"tiger",
"cat"
};
// Part 2: use HashSet constructor to ensure unique strings.
var hash = new HashSet<string>(array1);
// Part 3: convert to array of strings again.
string[] array2 = hash.ToArray();
// Part 4: display the resulting array.
Console.WriteLine(string.Join(",", array2));
}
}
Output
cat,dog,leopard,tiger
Next: The element 3 is in the HashSet. This means Overlaps returns true for array2, but false for array3.
C# program that uses Overlaps
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
int[] array1 = { 1, 2, 3 };
int[] array2 = { 3, 4, 5 };
int[] array3 = { 9, 10, 11 };
HashSet<int> set = new HashSet<int>(array1);
bool a = set.Overlaps(array2);
bool b = set.Overlaps(array3);
// Display results.
Console.WriteLine(a);
Console.WriteLine(b);
}
}
Output
True
False
Var: This example shows the use of the var-keyword. This simplifies the syntax of the HashSet declaration statement.
VarC# program that uses SymmetricExceptWith
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
char[] array1 = { 'a', 'b', 'c' };
char[] array2 = { 'b', 'c', 'd' };
var hash = new HashSet<char>(array1);
hash.SymmetricExceptWith(array2);
// Write char array.
Console.WriteLine(hash.ToArray());
}
}
Output
ad
Version 1: We test a HashSet(string). We add strings as keys and see if those keys exist.
Version 2: We use the Dictionary generic collection instead of a Hashset, and perform the same steps otherwise.
Result: The Dictionary had slightly better performance in this test than did the HashSet. In most tests the Dictionary was faster.
Thus: Dictionary should be used instead of HashSet in places where advanced HashSet functionality is not needed.
C# program that times HashSet performance
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
const int _max = 10000000;
static void Main()
{
var h = new HashSet<string>(StringComparer.Ordinal);
var d = new Dictionary<string, bool>(StringComparer.Ordinal);
var a = new string[] { "a", "b", "c", "d", "longer", "words", "also" };
var s1 = Stopwatch.StartNew();
// Version 1: use HashSet.
for (int i = 0; i < _max; i++)
{
foreach (string s in a)
{
h.Add(s);
h.Contains(s);
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use Dictionary.
for (int i = 0; i < _max; i++)
{
foreach (string s in a)
{
d[s] = true;
d.ContainsKey(s);
}
}
s2.Stop();
Console.WriteLine(h.Count);
Console.WriteLine(d.Count);
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.Read();
}
}
Output
7
7
529.99 ns HashSet
517.05 ns Dictionary
But: When the source input becomes large with thousands of elements, hashed collections are faster.
Dictionary vs. List