This chapter focuses on working with sets. The concepts of sets from both a mathematical definition and on the implementation level are described and explored. Common set operations, as well as their implementations, are covered in great detail. By end of this chapter, you will understand how to use JavaScript’s native Set object to utilize set operations.
Introducing Sets
Sets are one of the most fundamental data structures. The idea of a set is simple: it is a group of definite, distinct objects. In layman’s terms, in programming, a set is a group of unordered unique (no duplicate) elements. For example, a set of integers may be {1, 2, 3, 4}. Within this, its subsets are {}, {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}, {1, 2, 3}, {1, 2, 4}, {1, 3, 4}, and {2, 3, 4}. Sets are important for checking and adding a unique element in O(1) constant time. The reason that sets have constant time operations is that the implementations are based on that of hash tables (covered in Chapter 11).
The native Set object has only one property: size (integer). This property is the current number of elements within the set.
Set Operations
The set is a powerful data structure for performing uniqueness checks. This section will cover the following key operations: insertion, deletion, and contains.
Insertion
Notice that adding the duplicate element does not work for a set. As discussed in the introduction, insertion into a set occurs in constant time.
Time Complexity: O(1)
Deletion
This is useful for being able to delete items in constant time in contrast to arrays where it would take O(n) time to delete an item.
Time Complexity: O(1)
Contains
Time Complexity: O(1)
Other Utility Functions
In addition to the natively supported set functions, other essential operations are available; they are explored in this section.
Intersection
isSuperSet
Union
Difference
Summary
A set is a fundamental data structure to represent unordered unique elements. In this chapter, JavaScript’s native Set object was introduced. The Set object supports insertion, deletion, and contains check, which all have a time complexity of O(1). With these built-in methods, other fundamental set operations such as intersection, difference, union, and superset check are implemented. These will enable you to implement algorithms with fast uniqueness checks in future chapters.
Set Summary
Operation | Function Name | Description |
---|---|---|
Insertion | Set.add | Native JavaScript. Adds the element to the set if it’s not already in the set. |
Deletion | Set.delete | Native JavaScript. Deletes the element from the set if it’s in the set. |
Contains | Set.has | Native JavaScript. Checks whether an element exists within in the set. |
Intersection (A∩B) | intersectSets | Returns a set with common elements of set A and set B. |
Union (A∪B) | unionSet | Returns a set with all elements of set A and set B. |
Difference (A-B) | differenceSet | Returns a set with all elements. |
Exercises
USING SETS TO CHECK FOR DUPLICATES IN AN ARRAY
Time Complexity: O(n)
Space Complexity: O(n)
In an array of length n, this function has to iterate through the entire array in the worst case and also store all those elements in the set.
RETURNING ALL UNIQUE VALUES FROM SEPARATE ARRAYS
Given two integer arrays with some of the same values, return one array that has all the unique elements from both of the original arrays.
Time Complexity: O(n + m )
Space Complexity: O(n + m)
The time and space complexity for this algorithm is O(n + m) where n is the length of arr1 and m is the length of arr2. This is because all elements inside both arrays need to be iterated through.