You tin now usage nan JavaScript Set methods to execute group operations like intersection, union and more.
Sets are an basal information building successful immoderate programming language. Now you can use JavaScript's built-in methods to execute group operations. Simplify your set operations utilizing nan pursuing methods:
intersection()
intersection() returns a caller group containing elements successful some this group and nan fixed set.
const odds = new Set([1, 3, 5, 7, 9]); const squares = new Set([1, 4, 9]); console.log(odds.intersection(squares)); // Set(2) { 1, 9 }union()
union() returns a caller group containing each elements successful this group and nan fixed set.
const evens = new Set([2, 4, 6, 8]); const squares = new Set([1, 4, 9]); console.log(evens.union(squares)); // Set(6) { 2, 4, 6, 8, 1, 9 }difference()
difference() returns a caller group containing elements successful this group but not successful nan fixed set.
const odds = new Set([1, 3, 5, 7, 9]); const squares = new Set([1, 4, 9]); console.log(odds.difference(squares)); // Set(3) { 3, 5, 7 }symmetricDifference()
symmetricDifference() returns a caller group containing elements that are successful either this group aliases nan given set, but not successful both.
const evens = new Set([2, 4, 6, 8]); const squares = new Set([1, 4, 9]); console.log(evens.symmetricDifference(squares)); // Set(5) { 2, 6, 8, 1, 9 }isSubsetOf()
isSubsetOf() returns a boolean indicating if each elements of this group are successful nan fixed set.
const fours = new Set([4, 8, 12, 16]); const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]); console.log(fours.isSubsetOf(evens)); // trueisSupersetOf()
isSupersetOf() returns a boolean indicating if each elements of nan fixed group are successful this set.
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]); const fours = new Set([4, 8, 12, 16]); console.log(evens.isSupersetOf(fours)); // trueisDisjointFrom()
isDisjointFrom() Returns a boolean indicating if this group has nary elements successful communal pinch the given set.
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]); const squares = new Set([1, 4, 9, 16]); console.log(primes.isDisjointFrom(squares)); // trueUpdating your codification to usage nan built-in methods improves capacity and reduces technical debt.
Except arsenic different noted, nan contented of this page is licensed nether nan Creative Commons Attribution 4.0 License, and codification samples are licensed nether nan Apache 2.0 License. For details, spot nan Google Developers Site Policies. Java is simply a registered trademark of Oracle and/or its affiliates.
Last updated 2024-06-26 UTC.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing nan accusation I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too analyzable / excessively galore steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / codification issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }] { "lastModified": "Last updated 2024-06-26 UTC.", "confidential": False }