When storing data, the way in which you go about it is essential. In some cases, you may want to dump data into a storage to retrieve it later, while in others, you may want to set off data into different categories. In this blog, you will be introduced to the set data structure and its significance.
Data Structures -
Before understanding sets, we must learn about data structures.
- Data structures are a means of storing, sorting, and handling data.
- They allow you to efficiently manage data in ways suited for most tasks.
Sets -
- Sets are data structures that store unique, unordered values.
- Sets are good for comparison and checking membership, rather than retrieval and modification, as they don’t have a direct means of returning single values.
- How to use sets in JavaScript:
var set = new Set([1, ‘hi’, 0, 1]); // {1, ‘hi’, 0}
Implementation -
- Most sets are implemented through hash tables, which on average have an O(1) time complexity for insertions, deletions, and checks.
- Some sets are implemented through binary search trees, which on average have an O(log n) time complexity for the same functions.
- JavaScript uses hash tables.
Unique -
- Uniqueness is one of the main appeals of sets. If a duplicate value is passed into a set, the set will not accept the value.
- However, in JavaScript, a set can contain identical objects and arrays, as long as they have different references.
var set = new Set([{greeting: ‘hi’}]); // {{greeting: ‘hi’}}
set.add({greeting: ‘hi’}); // {{greeting: ‘hi’}, {greeting: ‘hi’}}
Unordered -
- Generally, sets don’t maintain any specific order.
- There aren’t any keys or indices representing values.
- The value itself is what identifies it. Though in JavaScript, sets maintain input order.
Basic Methods -
Here are some basic methods associated with sets:
- set.add(value) - adds values
- set.delete(value) - removes value
- set.has(value) - returns if a value exists
- set.size() - returns the size of the set
- set.clear() - removes all values from the set
- set.values() - returns the values of the set
Iteration -
To iterate through sets, use…
- set.forEach(callback(value, key, set)) - key is the same as value
- for (const val of set) {} Specific values can be found in sets through iteration.
var set = new Set([1, ‘hi’, 0, 1]); // {1, ‘hi’, 0}
set.forEach((val) => if(val === ‘hi’) {return val})
Other Methods -
Here are some methods that compare two sets:
- setA.union(setB) - returns a set containing elements found in either set
- setA.intersection(setB) - returns a set containing elements found in both sets
- setA.difference(setB) - returns a set containing elements not found in setB
- setA.symmetricDifference(setB) - returns a set containing elements not found in both sets
- setA.isSubsetOf(setB) - returns if setA’s elements are found in setB
- setA.isSupersetOf(setB) - returns if setB’s elements are found in setA
- setA.isDisjointFrom(setB) - returns if the sets share no elements
Sets to and from arrays -
var set = new Set([1, ‘hi’, 0, 1]); // {1, ‘hi’, 0}
var arr = [...set]; // [1, ‘hi’, 0]
Applications -
Sets may be preferable to other data structures when trying to…
- Store unique elements.
- Check for membership of elements.
- Compare and store differences/similarities of data.
With this information on the set data structure, you now have one more way of managing data at your disposal. Though sets are only one tool out of many, they can be the perfect solution to your data storage problems.
Top comments (0)