JavaScript

What is the JavaScript Equivalent to a C# HashSet

HashSet is an unordered group of unique/distinct elements in C#. It facilitates the implementation of sets and stores data in a hash table. This is a collection/group of the generic type. It is typically used to avoid adding duplicate elements to a collection.

This post will describe the data structure in JavaScript that is equivalent to the C# HashSet.

What is the JavaScript Equivalent to a C# HashSet?

A JavaScript “Set” is equivalent/same as a HashSet in C#. A JavaScript Set is a collection of unique/distinct values. Only one instance of each value is allowed in a Set. More specifically, a Set can contain any data type value, including object references or primitive values.

Syntax

For creating a Set in JavaScript, use the below syntax:

new Set()

“Set” Methods to Perform Various Operations

There are several ways to use the “Set” data structure in JavaScript to carry out different tasks.

  • add() method: It is used to add new elements in a Set. If the same element/object is already present in the Set, it does not add it again.
  • delete() method: For deleting any specified element, use the “delete()” method. After deleting, it outputs “true”.
  • clear() method: It removes/eliminates all elements from a Set.
  • has() method: For verifying whether the element exists in the Set or not, use the “has()” method.
  • size: It gives the number of all the existing elements of the Set.

Example 1: Add Elements in Set

Create a new Set using the “new Set()” constructor:

let jsSet = new Set();

Call the add() method to add elements in Set:

jsSet.add(8);

jsSet.add(20);

jsSet.add(14);

jsSet.add(23);

jsSet.add(11);

jsSet.add(11);

As you can see, we have added six elements in Set, but the output shows the “5” elements because Set does not contain duplicate values:

Example 2: Delete an Element From Set

Here, we will delete “14” from Set using the “delete()” method:

jsSet.delete(14);

The output shows “true”, which means “14” is successfully deleted from Set:

Example 3: Check if Set Contains Specific Element

Now, check whether “14” exists in Set or not. For that, call the “has()” method:

jsSet.has(14)

Output displays “false,” which means “14” is deleted from the created jsSet:

Example 4: Remove All Elements From Set

For removing all the elements from Set, use the “clear()” method:

jsSet.clear()

Example 5: Check Set Size

Check the size of the Set using the “size” property:

jsSet.size

The output shows “0”, which means all the elements of the Set are deleted:

That’s all about the JavaScript Sets.

Conclusion

A JavaScript “Set” is equivalent/same to a C# HashSet. HashSet is an unordered group of unique/distinct elements in C#. Similarly, the Set is a collection of unique values in JavaScript. Only one instance of each value is allowed in a Set. This post described the data structure in JavaScript that is equivalent to the C# HashSet.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.