How to implement your own set algorithm with JavaScript
Introduction
A set is a data structure that holds unique values in any order you wish. Sets are quite different from arrays in that they only allow non-repeated (unique values) within them.
Thankfully, programming languages tend to have the ability to convert the likes of an array into a set with minimal stress.
However, the aim of this shot is to devise a way that helps us find unique values with JavaScript and array methods. We will take a look at the set method and write our own JavaScript code.
The set data structure
The set data structure is initialized with a new keyword and an array is passed into it as a parameter.
If the array has repeating elements, only the unique elements are returned.
const arr = [1,2,2,3,3,4] //repeated valuesconst setVal = new Set(arr) //non-repeated, unique valuesconsole.log(setVal)//Set {1, 2, 3, 4}
How to find unique elements
We will pass the array to the checkItem function.
- We create an array called
newArrayin thecheckItemfunction. - We loop through the passed array.
- We check if the
newArrayincludes the given value initemsArrayto avoid repetition. - If
newArraydoes not include that value, we push the value into thenewArray. - Else we continue the loop without doing anything.
- In the end,
newArrayonly contains unique elements.
const checkItem = (itemsArray) => {let newArray = [];for (let i = 0; i < itemsArray.length; i++) {if (!newArray.includes(itemsArray[i])) {newArray.push(itemsArray[i]);} else {continue;}}console.log(newArray);};item=[1,2,2,3,4,4,3,5,6,5,6,7]checkItem(item)// This returns [1,2,3,4,5,6,7] i.e., only unique Values without repetition
Find the set intersection
const intersectSet = (arr1=[1,2,3,4], arr2=[1,3,4,5]) => {const newElem = [];for (let i = 0; i < arr2.length; i++) {if (arr1.includes(arr2[i])) {if(newElem.includes(arr2[i])){continue;}newElem.push(arr2[i]);} else {continue;}}console.log(newElem);};intersectSet();
We loop through the given array and check if an element of arr2 is present in arr1.
If it is, then we push the element (arr2[i]) to the newElem array. If arr1 doesn’t have an element in arr2, we continue the loop. To avoid repetition of the intersected element, we add an if conditional so that if the newElem already has the element we are trying to intersect, it doesn’t push it to the newElem array in order to avoid duplicates.
How to find the set union
A set union entails combining the given sets to form a set.
So, if we want to find the union of and , we have as the union.
//This function returns only unique elements in an arrayconst checkItem = (itemsArray) => {let newArray = [];for (let i = 0; i < itemsArray.length; i++) {if (!newArray.includes(itemsArray[i])) {newArray.push(itemsArray[i]);} else {continue;}}return newArray;};const unionArray = (arr1 = [2, 1, 12, 3, 4], arr2 = [1, 4, 5]) => {const newElem = [];newElem.push(...arr1);newElem.push(...arr2);let currValue = checkItem(newElem);console.log(currValue)};unionArray();
We take in two arrays as parameters, spread them, and push them to the newElem array. However, there will be repeated elements, so we use the checkItem function to remove all repeated elements in the set.
How to find the set difference
The difference between two sets, A and B (written as A-B), is the set of all elements of A that are not elements of B.
const arrDifference = (arr1 = [2, 1, 12, 3, 4], arr2 = [1, 4, 5]) => {let difference = arr1.filter((x) => !arr2.includes(x));console.log(difference);};arrDifference();
To find the difference between arr1 and arr2, we loop through arr1 and check for the elements that are not in arr2. Then, we append them to the difference array.
The filter runs the callback function, which gives a condition to filter the elements.