0

Possible Duplicate:
Is there a library for a Set data type in Javascript?

Is there a way to create a JavaScript data structure that mimics a c++ set? I need to perform searches in log(n) time, but can't find anything in the language that serves well. I've seen a couple of questions saying that I should represent the set as an object. Will that work? The key and payload of the array are numbers.

1

3 Answers 3

2

For unordered sets, you'll probably be better off with a hash table implementation. These do O(1) lookups, so long as the hash table doesn't get overloaded.

For ordered, in-memory sets, the standard answers seem to be treaps (good average time, high standard deviation) and red-black trees (poor average time, low standard deviation). These are both O(logn) lookup.

Sign up to request clarification or add additional context in comments.

Comments

0

It will have to, in javascript everything is an object.

4 Comments

What I meant was creating my own by doing something like var set = {};
yep, see Crockford on JavaScript - Chapter 2: And Then There Was JavaScript, and you WILL understand. Something to keep in mind: if needed, you check (set/convert) type of value before you use/read it, not while declaring the value.
Note that the main part of my question is if it's possible to search these objects or arrays in O(nlogn) time
then you might find this question on SO helpfull.
0

If you need ordered set (which allows you to loop from the smallest element to the largest element, in the order you define), you can implement your own data structure in JS. I can't give more information on this, since I haven't done this first hand.

If you are satisfied with unordered set, you can implement it as followed:

  1. Define a normalized String representation of the object to be stored in the set. If you need a set with number, just use the string representation of the number. If you need a set of user-defined object, you can pick out the properties that defines the identity of the object, and use them in the normalized String representation.
  2. Create a JS Object to use as set by mapping normalized String representation to the actual object.
  3. Searching can be done by checking the property name with the String representation.
  4. Insertion to the set can be done by: first check whether the String representation is already there by searching, then map the String representation to the actual object if the object is not yet in the set.
  5. Deletion can be done by using delete, with the property name being the String representation of the object to be deleted.

2 Comments

I'll try that, since I actually require an unordered set. Then I'm hoping I can implement a logarithmic time search method. Thanks
@Josh: For performance reason, accessing to Javascript property is at least O(log n). Depending on implementation, it might be better than that, but we at least expect a dictionary like structure to back the property lookup.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.