Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a data structure like the Java Set in JavaScript? [duplicate]

I want to use a data structure in JavaScript that can be used to store number of IDs. I should be able to check if a key already exists in that set, something like Java Sets.

I want to achive same behaviours as follows (this code is in Java):

Set<String> st = new HashSet<String>();
//add elemets

if(st.contains("aks") ){
  //do something
}

I want a JavaScript/dojo equivalent of the above code.

like image 825
aks Avatar asked Dec 03 '10 08:12

aks


People also ask

Is there a Set data structure in JavaScript?

Learn about how to use and when to use Set in JavaScript. The Set object allows you to create a collection of unique values (each value can occur only once). Set can contain any type of value (primitives or object reference).

What is the difference between Set and array in JavaScript?

The Set data type was introduced in ES2015 and the difference between array and set is that while an array can have duplicate values a set can't. Elements can be accessed in array using index which isn't possible in Set since it uses keys and elements can be traversed only in the way they were entered.

How many data structures are there in JavaScript?

8 Common Data Structures in JavaScript.

Is Set an array JS?

Sets give JavaScript developers a new way to represent data. While Arrays remain the general-purpose workhorse of JavaScript applications, Sets are intended to represent a unique collection of values.


1 Answers

I've written a JavaScript HashSet implementation that does what you want and allows any object to be a member of the set: http://code.google.com/p/jshashtable

However, if you just need to store strings, you could do something more simply by storing set members as property names of a normal Object. For example:

function StringSet() {
    var setObj = {}, val = {};

    this.add = function(str) {
        setObj[str] = val;
    };

    this.contains = function(str) {
        return setObj[str] === val;
    };

    this.remove = function(str) {
        delete setObj[str];
    };

    this.values = function() {
        var values = [];
        for (var i in setObj) {
            if (setObj[i] === val) {
                values.push(i);
            }
        }
        return values;
    };
}

A note about the implementation: val is an object used internally by the StringSet implementation that is unique to each set. Comparing property values of the object whose property names make up the set (setObj) against val eliminates the need for a hasOwnProperty() check and guarantees that only strings that have been added to the set will show up in values.

Example usage:

var set = new StringSet();
set.add("foo");
set.add("bar");

alert(set.contains("foo")); // true
alert(set.contains("baz")); // false

set.values(); // ["foo", "bar"], though not necessarily in that order
set.remove("foo");
set.values(); // ["bar"]
like image 82
Tim Down Avatar answered Oct 05 '22 14:10

Tim Down