What is JavaScript equivalent to C# HashSet? [SOLVED]


Written By - Olorunfemi Akinlua
Advertisement

Introduction

A HashSet is a data structure that stores data in an unordered fashion, using a hash function to map each data item to a unique key. This allows for fast insertion and retrieval of data, as well as for quick checking of whether a given data item is already in the set.

Hashsets are often used in applications where data needs to be stored in an unordered fashion, such as in a cache or in a set of values that need to be quickly checked for membership.

To create hashsets or HashSet behaviors, JavaScript has an object called Set. In this article, we will discuss how to create and use them.

 

Using Set to create HashSet behavior in JavaScript

In JavaScript, the Set object is a collection of unique values. It can be created by calling the Set constructor. The values in a Set can be of any type, including primitive values and objects.

The keys and elements have an insertion order based on the order through which they are added, and you can’t have the same key twice (unique key property).

You can create a Set object by passing an object to the Set constructor

const theSet = new Set([1, 2, 3, 4, 5]);
console.log(theSet);

Output

Set(5) { 1, 2, 3, 4, 5 }

Or we can add the elements singularly

Advertisement
let mySet = new Set();

mySet.add("one");
mySet.add("one");
mySet.add("two");
mySet.add("three");
mySet.add("four");
mySet.add("five");

console.log(mySet);

Output

Set(5) { 'one', 'two', 'three', 'four', 'five' }

You can see that even when we add two mySet.add("one") statement, we have only one "one" within the set.

A Set can be iterated over using forEach().

mySet.forEach(function (value) {
    console.log(value);
});

Output

one
two
three
four
five

According to MDN, internally, the Set object might be represented as a search tree with an O(log(N)) lookup time, a hash table with an O(1) lookup time, or any other data structure as long as the complexity is greater than O(N).

If you want to remove specified values from a Set object and check if a value is present within the Set object using the delete and has method. Here is an example

const theSet = new Set(["one", "two", "three", "four", "five"]);
console.log(theSet);

theSet.delete("five");
console.log(theSet);
console.log(theSet.has("four"));

Output

Set(5) { 'one', 'two', 'three', 'four', 'five' }
Set(4) { 'one', 'two', 'three', 'four' }
true

The Set object can have objects values and we can remove certain object values based on conditions as in the example code below where we remove the data that has its has property set to true.

Advertisement
const theSet = new Set();
theSet
    .add({ user: "jack", has: false })
    .add({ user: "Ox", has: true })
    .add({ user: "femi", has: false });

theSet.forEach((data) => {
    if (data.has === true) theSet.delete(data);
});

console.log(theSet);

Output

Set(2) { { user: 'jack', has: false }, { user: 'femi', has: false } }

 

Summary

If you need the HashSet functionality in JavaScript, you can make use of the Set object to achieve such. In addition, there are instance methods such as add, delete, has, and many more at your disposal.

 

References

Set - JavaScript | MDN (mozilla.org)
What is the JavaScript equivalent to a C# HashSet?

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment