{"id":77221,"date":"2022-11-21T08:00:51","date_gmt":"2022-11-21T07:00:51","guid":{"rendered":"https:\/\/code-maze.com\/?p=77221"},"modified":"2023-06-15T14:54:52","modified_gmt":"2023-06-15T12:54:52","slug":"csharp-hashset-vs-sortedset","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/","title":{"rendered":"HashSet vs SortedSet in C#"},"content":{"rendered":"<p>The HashSet&lt;T&gt; and SortedSet&lt;T&gt;\u00a0classes in the <code>System.Collections.Generic<\/code> namespace define two ways of storing and iterating over a collection of objects in C#. Both classes have pros and cons, so which one should we use?<\/p>\n<p>This article is going to compare and contrast the two types of collections and when it&#8217;s appropriate to use each.\u00a0<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/collections-csharp\/HashSetvsSortedSetInCSharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>So without further ado, let&#8217;s get started!<\/p>\n<h2><strong><a id=\"what-is-a-hashset\"><\/a>What Is a HashSet in C#<\/strong>?<\/h2>\n<p><strong>A<\/strong> <code>HashSet<\/code><strong> is a collection of unique elements that uses a<\/strong> <a href=\"https:\/\/code-maze.com\/csharp-queue-stack-hashtable\/#hashtable\" target=\"_blank\" rel=\"noopener\">hash table<\/a><strong> for storage, allowing faster retrieval of elements than other collection types<\/strong>. Adding and removing elements to the <code>HashSet<\/code> also has constant time complexity. However, it does not maintain insertion order and cannot access elements by index.<\/p>\n<p>To go into details about this collection, you can refer to this <a href=\"https:\/\/code-maze.com\/csharp-hashset\/\" target=\"_blank\" rel=\"noopener\">article<\/a> to learn more about how <code>HashSet<\/code> works in C#.<\/p>\n<h2><a id=\"what-is-a-sortedset\"><\/a>What Is a SortedSet in C#?<\/h2>\n<p>A <code>SortedSet<\/code>, in C#, is <strong>a collection of unique elements sorted according to their natural ordering or a specified comparator<\/strong>. It allows for fast retrieval of elements and efficient operations on subsets.<\/p>\n<p>One notable feature of the <code>SortedSet<\/code> is its ability to efficiently perform set operations such as union, intersection, and difference, which makes it particularly useful for tasks such as creating a list of unique elements from multiple sets.<\/p>\n<p>A <code>SortedSet<\/code> <strong>does not allow null values or permit duplicate elements<\/strong>. Trying to add a duplicate element <strong>will not affect the set<\/strong>.\u00a0<\/p>\n<p>Please refer to this <a href=\"https:\/\/code-maze.com\/csharp-sortedset\/\" target=\"_blank\" rel=\"noopener\">article<\/a> to learn more about how <code>SortedSet<\/code> works in C#.<\/p>\n<h2><a id=\"similarities\"><\/a>Similarities Between a HashSet and a SortedSet<\/h2>\n<p><code>HashSet&lt;T&gt;<\/code> and <code>SortedSet&lt;T&gt;<\/code> objects store unique elements because they implement the <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.iset-1?view=net-6.0\" target=\"_blank\" rel=\"nofollow noopener\">ISet&lt;T&gt;<\/a>\u00a0interface.\u00a0<\/p>\n<p>Both data structures offer better <a href=\"#benchmarks\">performance<\/a> when adding, retrieving, and searching operations than other collections, such as <a href=\"https:\/\/code-maze.com\/csharp-list-collection\/\" target=\"_blank\" rel=\"noopener\">lists<\/a> and <a href=\"https:\/\/code-maze.com\/csharp-basics-arrays\/\" target=\"_blank\" rel=\"noopener\">arrays<\/a>.\u00a0<\/p>\n<h2><a id=\"differences\"><\/a>Differences Between a HashSet and a SortedSet<\/h2>\n<p><code>HashSet<\/code> and <code>SortedSet<\/code> objects use different data structures to store data.\u00a0 \u00a0<\/p>\n<p>The <code>HashSet&lt;T&gt;<\/code> class uses a hash table to organize its elements. In contrast, a <code>SortedSet&lt;T&gt;<\/code> class uses a balanced binary tree data structure to maintain elements in a specific order. Therefore, the <code>SortedSet&lt;T&gt;<\/code> class has slower retrieval operations than a <code>HashSet&lt;T&gt;<\/code> class but can easily access elements in a specific order.<\/p>\n<p>A <code>SortedSet&lt;T&gt;<\/code> can retrieve a subset of elements within a specific range using the <code>SortedSet&lt;T&gt;.GetViewBetween(T, T)<\/code> method, which is not available in the <code>HashSet&lt;T&gt;<\/code> class.\u00a0<\/p>\n<p>Additionally, <code>SortedSet&lt;T&gt;<\/code> allows us to retrieve the minimum and maximum elements through its <code>SortedSet&lt;T&gt;.Max<\/code> and\u00a0 <code>SortedSet&lt;T&gt;.Min<\/code> properties, while the <code>HashSet&lt;T&gt;<\/code> class lacks them. It achieves this by implementing the IComparer&lt;T&gt; interface.\u00a0<\/p>\n<p>Let&#8217;s analyze how <code>HashSet<\/code> and <code>SortedSet<\/code> objects perform theoretically and then use benchmarks to test those theories.<\/p>\n<h2><a id=\"big-o\"><\/a>Big O Analysis<\/h2>\n<p>Big O analysis is a way of determining the efficiency and complexity of how an algorithm performs. It specifically looks at the worst-case scenario for the algorithm, as that will give the upper bound on its runtime.<\/p>\n<p><strong>So, how do HashSet and SortedSet objects perform?<\/strong><\/p>\n<p>We can use Big O analysis to assess how the data structures perform when handling crucial CRUD operations:<\/p>\n\n<table id=\"tablepress-44\" class=\"tablepress tablepress-id-44\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">Operation<\/th><th class=\"column-2\">HashSet<\/th><th class=\"column-3\">SortedSet<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">Iteration<\/td><td class=\"column-2\">O(N)<\/td><td class=\"column-3\">O(N)<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">Search<\/td><td class=\"column-2\">O(1)<\/td><td class=\"column-3\">O(Log N)<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">Insertion<\/td><td class=\"column-2\">O(1)<\/td><td class=\"column-3\">O(Log N)<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">Removing Elements<\/td><td class=\"column-2\">O(1)<\/td><td class=\"column-3\">O(Log N)<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">Enumerating Elements in Sorted Order<\/td><td class=\"column-2\">O(N Log N)<\/td><td class=\"column-3\">O(N)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-44 from cache -->\n<p><code>HashSet&lt;T&gt;<\/code> and <code>SortedSet&lt;T&gt;<\/code> classes perform similarly when iterating through elements, as the operation depends on the input size, hence, has a performance of O(N).<\/p>\n<p>Additionally, the <code>HashSet&lt;T&gt;<\/code> is faster than a <code>SortedSet&lt;T&gt;<\/code> when searching elements, as <strong>it has a constant time lookup performance of O(1), while the latter has O(N) performance<\/strong> assuming the last element is the one we are looking for.\u00a0<\/p>\n<p>When adding or removing elements, the <code>HashSet&lt;T&gt;<\/code> class performs better than the <code>SortedSet&lt;T&gt;<\/code> class. The <code>HashSet&lt;T&gt;<\/code> class uses a hash table to organize its elements; inserting an element is an <strong>O(1) operation<\/strong>. On the other hand, the <code>SortedSet&lt;T&gt;<\/code> uses a <a href=\"https:\/\/code-maze.com\/csharp-heap-sort\/\" target=\"_blank\" rel=\"noopener\">binary tree<\/a> with a Root node and a Left and Right node on every node instance.\u00a0Since the <code>SortedSet&lt;T&gt;<\/code> class ensures that the elements are in the correct order, every insertion operation places the new element in the correct location in the set, making it an <strong>O(log N) operation<\/strong>.<\/p>\n<p>The <code>HashSet&lt;T&gt;<\/code> class <strong>performs poorly when enumerating elements, as we can&#8217;t access them by their indices<\/strong>. We may be forced to use an enumerator or copy the object to a different collection, such as a list, to enumerate its elements, which takes <strong>O(N log N)<\/strong> time. On the other hand, the elements in the <code>SortedSet&lt;T&gt;<\/code> objects are in the <strong>correct order; hence, enumeration processes take O(N) time<\/strong>.<\/p>\n<p>Without further ado, let&#8217;s test how these classes perform!<\/p>\n<h2><a id=\"benchmarks\"><\/a>Performance Benchmarks<\/h2>\n<p>First, let&#8217;s implement a function that returns a list of integers. We are going to use it to initialize our <code>SortedSet<\/code> and <code>HashSet<\/code> objects:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private List&lt;int&gt; RandomInts(int size)\r\n{\r\n    var rand = new Random();\r\n    var numbers = new List&lt;int&gt;();\r\n\r\n    for (int i = 0; i &lt; size - 1; i++)\r\n    {\r\n        numbers.Add(rand.Next());\r\n    }\r\n\r\n    numbers.Add(Int32.MaxValue - 1);\r\n\r\n    return numbers;\r\n}<\/pre>\n<p>Our <code>RandomInts()<\/code> method takes the number of integers to generate as its sole input. It uses the inbuilt <a href=\"https:\/\/code-maze.com\/csharp-random-class\/\" target=\"_blank\" rel=\"noopener\">random<\/a> class to generate random numbers and inserts each value into a <code>List&lt;int&gt;<\/code> object before returning it. Also, we add an integer <code>Int32.MaxValue - 1<\/code>, which we are going to use later.\u00a0<\/p>\n<p>Next, let&#8217;s initialize our <code>SortedSet<\/code> and <code>HashSet<\/code> objects by using our <code>RandomInts()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private readonly List&lt;int&gt; _numList;\r\nprivate readonly HashSet&lt;int&gt; _hashSet;\r\nprivate readonly SortedSet&lt;int&gt; _sortedSet;\r\nprivate readonly int _searchValue;\r\n\r\npublic Operations()\r\n{\r\n    _numList = RandomInts(1000000);\r\n    _hashSet = InitializeIntHashSet();\r\n    _sortedSet = InitializeIntSortedSet();\r\n    _searchValue = Int32.MaxValue - 1;\r\n}\r\n\r\npublic HashSet&lt;int&gt; InitializeIntHashSet() \r\n{\r\n    var hashSet = new HashSet&lt;int&gt;();\r\n\r\n    foreach (var number in _numList) \r\n    {\r\n        hashSet.Add(number);\r\n    }\r\n\r\n    return hashSet;\r\n}\r\n\r\npublic SortedSet&lt;int&gt; InitializeIntSortedSet()\r\n{\r\n    var sortedSet = new SortedSet&lt;int&gt;();\r\n\r\n    foreach (var number in _numList)\r\n    {\r\n        sortedSet.Add(number);\r\n    }\r\n\r\n    return sortedSet;\r\n}<\/pre>\n<p>Here, we populate the <code>HashSet&lt;int&gt;<\/code> and <code>SortedSet&lt;int&gt;<\/code> objects with one million integers, which <strong>covers the iteration and insertion of elements operations<\/strong>. However, we must remember that these data structures hold unique elements; hence, the objects may have fewer values if <code>_numList<\/code> contains duplicates.\u00a0<\/p>\n<h3><a id=\"search-remove-implementation\"><\/a>Search and Remove Elements From the HashSet and SortedSet<\/h3>\n<p>Let&#8217;s check whether an element exists in our <code>SortedSet<\/code> and <code>HashSet<\/code> objects by invoking the <code>Contains()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public bool SearchSortedSet()\r\n{\r\n    return _sortedSet.Contains(_searchValue);\r\n}\r\n\r\npublic bool SearchHashSet()\r\n{\r\n    return _hashSet.Contains(_searchValue);\r\n}<\/pre>\n<p>We expect these methods to return true as <code>_searchValue<\/code> is present in our <code>SortedSet<\/code> and <code>HashSet<\/code> objects.<\/p>\n<p>Next, let&#8217;s implement functions to remove elements by invoking the <code>Remove()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public HashSet&lt;int&gt; RemoveElementFromHashSet()\r\n{\r\n    _hashSet.Remove(_searchValue);\r\n\r\n    return _hashSet;\r\n}\r\n\r\npublic SortedSet&lt;int&gt; RemoveElementFromSortedSet()\r\n{\r\n    _sortedSet.Remove(_searchValue);\r\n\r\n    return _sortedSet;\r\n}<\/pre>\n<p>Since both the <code>HashSet&lt;T&gt;<\/code> and <code>SortedSet&lt;T&gt;<\/code> classes support the <code>RemoveWhere(Predicate T)<\/code> method, let&#8217;s implement these functions:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public HashSet&lt;int&gt; RemoveWhereFromHashSet()\r\n{\r\n    _hashSet.RemoveWhere(IsOdd);\r\n\r\n    return _hashSet;\r\n}\r\n\r\npublic SortedSet&lt;int&gt; RemoveWhereFromSortedSet()\r\n{\r\n    _sortedSet.RemoveWhere(IsOdd);\r\n\r\n    return _sortedSet;\r\n}\r\n\r\nprivate bool IsOdd(int num)\r\n{\r\n    return num % 2 == 1;\r\n}<\/pre>\n<p>We use the <code>IsOdd()<\/code> method as our <a href=\"https:\/\/code-maze.com\/delegates-charp\/\" target=\"_blank\" rel=\"noopener\">predicate<\/a> for the <code>RemoveWhere()<\/code> method, which removes all the odd numbers from both objects.\u00a0<\/p>\n<h3><a id=\"enumerating-elements\"><\/a>How to Sort Elements in a Sorted Order<\/h3>\n<p>Sometimes, we need to sort the elements in our <code>SortedSet<\/code> or <code>HashSet<\/code> objects in a specific order:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public List&lt;int&gt; SortHashSetElements()\r\n{\r\n    var sortElements = _hashSet.OrderBy(element =&gt; element).ToList();\r\n\r\n    return sortElements;\r\n}\r\n\r\npublic List&lt;int&gt; SortSortedSetElements()\r\n{\r\n    return _sortedSet.ToList();\r\n}<\/pre>\n<p>Since the elements in the <code>_sortedSet<\/code> are in the correct order, we invoke the <code>ToList()<\/code> method to enumerate them.<\/p>\n<p>But, for <code>_heshSet<\/code>, we use the <code>OrderBy()<\/code> method to sort the elements in ascending order in our <code>_hashSet<\/code>.<\/p>\n<p>Alternatively, we can convert the <code>_hashSet<\/code> object into different collections, such as arrays, before sorting them:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var array = _hashSet.ToArray();\r\nArray.Sort(array);<\/pre>\n<h3><a id=\"performance-results\"><\/a>Performance Results<\/h3>\n<p>Finally, let&#8217;s assess how the <code>SortedSet<\/code> and <code>HashSet<\/code> objects perform when performing our CRUD operations:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">|                     Method |                 Mean |             StdDev |               Median | Allocated |\r\n|--------------------------- |---------------------:|-------------------:|---------------------:|----------:|\r\n|       InitializeIntHashSet |   147,134,671.465 ns |  14,192,012.583 ns |   150,572,175.000 ns |43111216 B |\r\n|     InitializeIntSortedSet | 1,285,055,140.000 ns | 127,630,638.873 ns | 1,276,749,550.000 ns |39991392 B |\r\n|                            |                      |                    |                      |           |\r\n|   RemoveElementFromHashSet |            10.007 ns |           2.159 ns |             9.582 ns |         - |\r\n| RemoveElementFromSortedSet |           287.854 ns |          37.483 ns |           278.201 ns |         - |\r\n|                            |                      |                    |                      |           |\r\n|     RemoveWhereFromHashSet |     7,580,281.445 ns |     415,922.431 ns |     7,480,238.281 ns |      70 B |\r\n|   RemoveWhereFromSortedSet |    49,730,196.986 ns |   5,309,548.323 ns |    47,545,329.167 ns | 4098112 B |\r\n|                            |                      |                    |                      |           |\r\n|              SearchHashSet |             8.271 ns |           1.907 ns |             7.558 ns |         - |\r\n|\u00a0           SearchSortedSet |           118.102 ns |           1.664 ns |           117.925 ns |         - | \r\n|                            |                      |                    |                      |           |   \r\n|      SortSortedSetElements |    87,276,315.625 ns |   1,674,766.333 ns |    86,826,458.333 ns | 4001217 B |\r\n|        SortHashSetElements |   492,307,633.000 ns |  89,256,871.787 ns |   492,247,450.000 ns |15997040 B |<\/pre>\n<p>We can see that the <code>HashSet&lt;T&gt;<\/code> class is faster than the <code>SortedSet&lt;T&gt;<\/code> classes in <strong>search, addition, and data removal processes<\/strong>. The <code>HashSet&lt;T&gt;<\/code> class performs better in these operations since\u00a0it uses a hash table instead of a binary tree in a <code>SortedSet&lt;T&gt;<\/code> object as its underlying data structure.\u00a0<\/p>\n<p>Although the test results show the <code>SortedSet&lt;T&gt;<\/code> class performs better than the <code>HashSet&lt;T&gt;<\/code> class when enumerating elements in sorted order, we should remember that the <strong>sorting process takes place when initializing<\/strong> <strong>the<\/strong> <code>SortedSet&lt;T&gt;<\/code>,<strong> which is slower than initializing the <code>HashSet&lt;T&gt;<\/code> class.\u00a0<\/strong>Therefore, to sort elements in the <code>SortedSet&lt;T&gt;<\/code> object, we simply convert it into a <code>List&lt;T&gt;<\/code>, while in the <code>HashSet&lt;T&gt;<\/code> object, we have to order the elements before adding them into a <code>List&lt;T&gt;<\/code>.\u00a0<\/p>\n<h2><a id=\"conclusion\"><\/a>Conclusion<\/h2>\n<p>So, in this article, we learn the similarities and differences between the HashSet&lt;T&gt; and SortedSet&lt;T&gt; classes in C#.<\/p>\n<p>The <code>HashSet&lt;T&gt;<\/code> class is the way to go if we <strong>need to store unique elements and not care about their order<\/strong>. However, the <code>SortedSet&lt;T&gt;<\/code> class is our best bet for sorting unique data while adding elements.\u00a0<\/p>\n<p>These collections have different <a href=\"#benchmarks\">performance<\/a> characteristics depending on how we intend to use them. The <code>HashSet&lt;T&gt;<\/code> class is generally faster for lookup operations, while the <code>SortedSet&lt;T&gt;<\/code> class has better <a href=\"#enumerating-elements\">enumeration<\/a> performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The HashSet&lt;T&gt; and SortedSet&lt;T&gt;\u00a0classes in the System.Collections.Generic namespace define two ways of storing and iterating over a collection of objects in C#. Both classes have pros and cons, so which one should we use? This article is going to compare and contrast the two types of collections and when it&#8217;s appropriate to use each.\u00a0 So [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1476,1477,1487],"class_list":["post-77221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-hashset","tag-hashset-in-c","tag-sortedset","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HashSet vs SortedSet in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we are going to compare the HashSet and SortedSet classes in C#, looking at the similarities and differences.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HashSet vs SortedSet in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to compare the HashSet and SortedSet classes in C#, looking at the similarities and differences.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-21T07:00:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-15T12:54:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"HashSet vs SortedSet in C#\",\"datePublished\":\"2022-11-21T07:00:51+00:00\",\"dateModified\":\"2023-06-15T12:54:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\"},\"wordCount\":1218,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"hashset\",\"hashset in c#\",\"sortedset\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\",\"url\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\",\"name\":\"HashSet vs SortedSet in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-11-21T07:00:51+00:00\",\"dateModified\":\"2023-06-15T12:54:52+00:00\",\"description\":\"In this article, we are going to compare the HashSet and SortedSet classes in C#, looking at the similarities and differences.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HashSet vs SortedSet in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HashSet vs SortedSet in C# - Code Maze","description":"In this article, we are going to compare the HashSet and SortedSet classes in C#, looking at the similarities and differences.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/","og_locale":"en_US","og_type":"article","og_title":"HashSet vs SortedSet in C# - Code Maze","og_description":"In this article, we are going to compare the HashSet and SortedSet classes in C#, looking at the similarities and differences.","og_url":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/","og_site_name":"Code Maze","article_published_time":"2022-11-21T07:00:51+00:00","article_modified_time":"2023-06-15T12:54:52+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"HashSet vs SortedSet in C#","datePublished":"2022-11-21T07:00:51+00:00","dateModified":"2023-06-15T12:54:52+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/"},"wordCount":1218,"commentCount":2,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["hashset","hashset in c#","sortedset"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/","url":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/","name":"HashSet vs SortedSet in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-11-21T07:00:51+00:00","dateModified":"2023-06-15T12:54:52+00:00","description":"In this article, we are going to compare the HashSet and SortedSet classes in C#, looking at the similarities and differences.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-hashset-vs-sortedset\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"HashSet vs SortedSet in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/77221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=77221"}],"version-history":[{"count":7,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/77221\/revisions"}],"predecessor-version":[{"id":94979,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/77221\/revisions\/94979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=77221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=77221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=77221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}