Skip to content

Instantly share code, notes, and snippets.

@johannesfk
Created March 5, 2024 14:45
Show Gist options
  • Save johannesfk/34170889884a4daa3fca64660ce8794d to your computer and use it in GitHub Desktop.
Save johannesfk/34170889884a4daa3fca64660ce8794d to your computer and use it in GitHub Desktop.
Hashset vs List in C#

Comparing Performance

Hashsets are generally faster than lists. The major reason this is the case is that no duplicated data is allowed. In the case of a contains operation there is an overhead by using hashsets. This is because a hash needs to be calculated for the item that will be queried. Hashing is an slighty expensive operation, so lists are faster with fewer items because the time to calculate the hash is longer. However, on a certain point hashsets and lists break even and the performance of lists will continue to decrease as more items are added.

using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace CS_Using_HashSet
{
class Program
{
static string[] names = new string[] {
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita",
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis", "Leena",
"Neema", "Sita" , "Tejas", "Mahesh", "Ramesh", "Ram",
"GundaRam", "Sabnis", "Leena", "Neema", "Sita" ,
"Tejas", "Mahesh", "Ramesh", "Ram", "GundaRam",
"Sabnis", "Leena", "Neema", "Sita" , "Tejas",
"Mahesh", "Ramesh", "Ram", "GundaRam", "Sabnis",
"Leena", "Neema", "Sita"
};
static void Main(string[] args)
{
string[] names = new string[] {
"mahesh",
"vikram",
"mahesh",
"mayur",
"suprotim",
"saket",
"manish"
};
//2. Length of Array and Printing array
Console.WriteLine("Length of Array: " + names.Length);
Console.WriteLine();
Console.WriteLine("The Data in Array");
foreach (var n in names)
{
Console.WriteLine(n);
}
Console.WriteLine();
//3. Defining HashSet by passing an Array of string to it
HashSet<string> hSet = new HashSet<string>(names);
//4. Count of Elements in HashSet
Console.WriteLine("Count of Data in HashSet: " + hSet.Count);
Console.WriteLine();
//5. Printing Data in HashSet, this will eliminate duplication of "mahesh"
Console.WriteLine("Data in HashSet");
foreach (var n in hSet)
{
Console.WriteLine(n);
}
string[] names1 = new string[] {
"mahesh","sabnis","manish","sharma","saket","karnik"
};
string[] names2 = new string[] {
"suprotim","agarwal","vikram","pendse","mahesh","mitkari"
};
//3. Using UnionWith
HashSet<string> hSetN1 = new HashSet<string>(names1);
Console.WriteLine("Data in First HashSet");
foreach (var n in hSetN1)
{
Console.WriteLine(n);
}
Console.WriteLine("_______________________________________________________________");
HashSet<string> hSetN2 = new HashSet<string>(names2);
Console.WriteLine("Data in Second HashSet");
foreach (var n in hSetN2)
{
Console.WriteLine(n);
}
hSetN1.UnionWith(hSetN2);
Console.WriteLine("Count of Data in HashSet after union " + hSetN1.Count);
Console.WriteLine("Data After Union");
foreach (var n in hSetN1)
{
Console.WriteLine(n);
}
// Using ExpectWith
Console.WriteLine();
Console.WriteLine("_________________________________");
Console.WriteLine("Data in HashSet before using Except With");
HashSet<string> hSetN3 = new HashSet<string>(names1);
foreach (var n in hSetN3)
{
Console.WriteLine(n);
}
Console.WriteLine();
Console.WriteLine("_________________________________");
Console.WriteLine("Using Except With");
hSetN3.ExceptWith(hSetN2);
foreach (var n in hSetN3)
{
Console.WriteLine(n);
}
// Using SymmetricExceptWith
Console.WriteLine();
Console.WriteLine("_________________________________");
Console.WriteLine("Data in HashSet before using SymmetricExceptWith");
Console.WriteLine();
Console.WriteLine("HashSet 1");
HashSet<string> hSetN4 = new HashSet<string>(names1);
foreach (var n in hSetN4)
{
Console.WriteLine(n);
}
Console.WriteLine();
Console.WriteLine("HashSet 2");
foreach (var n in hSetN2)
{
Console.WriteLine(n);
}
Console.WriteLine();
Console.WriteLine("_________________________________");
Console.WriteLine("Using SymmetricExceptWith");
hSetN4.SymmetricExceptWith(hSetN2);
foreach (var n in hSetN4)
{
Console.WriteLine(n);
}
Get_Add_Performance_HashSet_vs_List();
Get_Contains_Performance_HashSet_vs_List();
Get_Remove_Performance_HashSet_vs_List();
}
static void Get_Add_Performance_HashSet_vs_List()
{
Console.WriteLine("____________________________________");
Console.WriteLine("List Performance while Adding Item");
Console.WriteLine();
List<string> lstNames = new List<string>();
var s2 = Stopwatch.StartNew();
foreach (string s in names)
{
lstNames.Add(s);
}
s2.Stop();
Console.WriteLine(s2.Elapsed.TotalMilliseconds.ToString("0.000 ms")); Console.WriteLine();
Console.WriteLine("Ends Here");
Console.WriteLine();
Console.WriteLine("____________________________________");
Console.WriteLine("HashSet Performance while Adding Item");
Console.WriteLine();
HashSet<string> hStringNames = new HashSet<string>(StringComparer.Ordinal);
var s1 = Stopwatch.StartNew();
foreach (string s in names)
{
hStringNames.Add(s);
}
s1.Stop();
Console.WriteLine(s1.Elapsed.TotalMilliseconds.ToString("0.000 ms")); Console.WriteLine();
Console.WriteLine("Ends Here");
Console.WriteLine("____________________________________");
Console.WriteLine();
}
static void Get_Contains_Performance_HashSet_vs_List()
{
Console.WriteLine("____________________________________");
Console.WriteLine("List Performance while checking Contains operation");
Console.WriteLine();
List<string> lstNames = new List<string>();
var s2 = Stopwatch.StartNew();
foreach (string s in names)
{
lstNames.Contains(s);
}
s2.Stop();
Console.WriteLine(s2.Elapsed.TotalMilliseconds.ToString("0.000 ms")); Console.WriteLine();
Console.WriteLine("Ends Here");
Console.WriteLine();
Console.WriteLine("____________________________________");
Console.WriteLine("HashSet Performance while checking Contains operation");
Console.WriteLine();
HashSet<string> hStringNames = new HashSet<string>(StringComparer.Ordinal);
var s1 = Stopwatch.StartNew();
foreach (string s in names)
{
hStringNames.Contains(s);
}
s1.Stop();
Console.WriteLine(s1.Elapsed.TotalMilliseconds.ToString("0.000 ms"));
Console.WriteLine();
Console.WriteLine("Ends Here");
Console.WriteLine("____________________________________");
Console.WriteLine();
}
static void Get_Remove_Performance_HashSet_vs_List()
{
Console.WriteLine("____________________________________");
Console.WriteLine("List Performance while performing Remove item operation");
Console.WriteLine();
List<string> lstNames = new List<string>();
var s2 = Stopwatch.StartNew();
foreach (string s in names)
{
lstNames.Remove(s);
}
s2.Stop();
Console.WriteLine(s2.Elapsed.TotalMilliseconds.ToString("0.000 ms")); Console.WriteLine();
Console.WriteLine("Ends Here");
Console.WriteLine();
Console.WriteLine("____________________________________");
Console.WriteLine("HashSet Performance while performing Remove item operation");
Console.WriteLine();
HashSet<string> hStringNames = new HashSet<string>(StringComparer.Ordinal);
var s1 = Stopwatch.StartNew();
foreach (string s in names)
{
hStringNames.Remove(s);
}
s1.Stop();
Console.WriteLine(s1.Elapsed.TotalMilliseconds.ToString("0.000 ms")); Console.WriteLine();
Console.WriteLine("Ends Here");
Console.WriteLine("____________________________________");
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment