Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js set utils

function isSuperset(set, subset) {
    for (let elem of subset) {
        if (!set.has(elem)) {
            return false
        }
    }
    return true
}

function union(setA, setB) {
    let _union = new Set(setA)
    for (let elem of setB) {
        _union.add(elem)
    }
    return _union
}

function intersection(setA, setB) {
    let _intersection = new Set()
    for (let elem of setB) {
        if (setA.has(elem)) {
            _intersection.add(elem)
        }
    }
    return _intersection
}

function symmetricDifference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        if (_difference.has(elem)) {
            _difference.delete(elem)
        } else {
            _difference.add(elem)
        }
    }
    return _difference
}

function difference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        _difference.delete(elem)
    }
    return _difference
}

// Examples
const setA = new Set([1, 2, 3, 4])
const setB = new Set([2, 3])
const setC = new Set([3, 4, 5, 6])

isSuperset(setA, setB)          // returns true
union(setA, setC)               // returns Set {1, 2, 3, 4, 5, 6}
intersection(setA, setC)        // returns Set {3, 4}
symmetricDifference(setA, setC) // returns Set {1, 2, 5, 6}
difference(setA, setC)          // returns Set {1, 2}
Comment

PREVIOUS NEXT
Code Example
Javascript :: vuejs check word is availble in the string or not 
Javascript :: id always returing null angular 
Javascript :: reverse not working react 
Javascript :: battery status check on user machine 
Javascript :: javascript node retry promise.all 
Javascript :: Arrow functions by Codeacademy 
Javascript :: "date change error" 
Javascript :: Using toLocaleString() to Print JavaScript Number Format with Commas 
Javascript :: join () method to join all elements of the array into a string to reverse an string 
Javascript :: Array helper functions in ES6 
Javascript :: how add element at beginning of array in javascript using splice 
Javascript :: what does react js allows us to do 
Javascript :: unique elements 
Javascript :: add value get value 
Javascript :: reset default style javascript 
Javascript :: elementor slider javascript edit 
Javascript :: open failed: EACCES (Permission denied) react native 
Javascript :: console form elememts as json object 
Javascript :: netlify not deploying react site 
Javascript :: how to add edit and delete rows of a html table with javascript 
Javascript :: whatisjsx 
Javascript :: filter list of array if not true return default array 
Javascript :: vue js key modifiers 
Javascript :: ProgressBar from color to color 
Javascript :: hide react from netlify 
Javascript :: path error 
Javascript :: VueJS - getting the last element of a split string array 
Javascript :: android MediaController audio example 
Javascript :: sending string from jquery ajax to asp.net mvc controller. 
Javascript :: javascript element.children.forEach not working 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =