@rbxts/jsnatives
intersection
Creates a new Set containing only elements that exist in both sets.
Signature
function intersection<T>(set1: Set<T>, set2: Set<T>): Set<T>
Description
The intersection
function creates a new Set containing only the elements that are present in both of the input sets. It returns the common elements between the two sets.
Parameters
set1
: The first Set.set2
: The second Set.
Return value
- A new Set containing only the elements that exist in both
set1
andset2
.
Examples
Basic usage
// Create two sets with some common elementsconst set1 = new Set([1, 2, 3, 4]);const set2 = new Set([3, 4, 5, 6]);
// Find the common elements using intersectionconst result = SetUtils.intersection(set1, set2);console.log([...result]); // Outputs: [3, 4]
With string elements
// Sets with string elementsconst fruits1 = new Set(["apple", "banana", "orange"]);const fruits2 = new Set(["banana", "grape", "orange"]);
// Find common fruitsconst commonFruits = SetUtils.intersection(fruits1, fruits2);console.log([...commonFruits]); // Outputs: ["banana", "orange"]
No common elements
// Sets with no elements in commonconst set1 = new Set([1, 2, 3]);const set2 = new Set([4, 5, 6]);
// Intersection will be emptyconst emptyIntersection = SetUtils.intersection(set1, set2);console.log([...emptyIntersection]); // Outputs: []console.log(emptyIntersection.size); // Outputs: 0
Empty set cases
// Intersection with an empty setconst numbers = new Set([1, 2, 3]);const empty = new Set();
// Intersection with empty set always results in empty setconst result1 = SetUtils.intersection(numbers, empty);console.log([...result1]); // Outputs: []
// Intersection of two empty sets is also emptyconst result2 = SetUtils.intersection(empty, empty);console.log([...result2]); // Outputs: []
Practical application
// Finding common permissions between rolesfunction findCommonPermissions(role1Permissions: Set<string>, role2Permissions: Set<string>): Set<string> { return SetUtils.intersection(role1Permissions, role2Permissions);}
// Editor role permissionsconst editorPermissions = new Set(["read", "write", "comment", "edit"]);
// Moderator role permissionsconst moderatorPermissions = new Set(["read", "comment", "moderate", "delete"]);
// Find permissions common to both rolesconst sharedPermissions = findCommonPermissions(editorPermissions, moderatorPermissions);console.log([...sharedPermissions]);// Outputs: ["read", "comment"]
Using with large data sets
// Example with larger setsfunction findMatchingProducts(userInterests: Set<string>, availableProducts: Set<string>): Set<string> { return SetUtils.intersection(userInterests, availableProducts);}
// User has expressed interest in these product categoriesconst userInterests = new Set([ "electronics", "books", "clothing", "sports", "outdoors", "kitchen"]);
// Currently available product categories on saleconst saleProducts = new Set([ "electronics", "furniture", "kitchen", "bathroom", "clothing", "garden"]);
// Find matching interests and sale itemsconst recommendedProducts = findMatchingProducts(userInterests, saleProducts);console.log([...recommendedProducts]);// Outputs: ["electronics", "clothing", "kitchen"]
Comparison with manual implementation
// Manual implementation of intersection operationfunction manualIntersection<T>(set1: Set<T>, set2: Set<T>): Set<T> { const result = new Set<T>(); for (const item of set1) { if (set2.has(item)) { result.add(item); } } return result;}
// Using SetUtils.intersectionconst set1 = new Set([1, 2, 3, 4]);const set2 = new Set([3, 4, 5, 6]);
const manualResult = manualIntersection(set1, set2);const utilResult = SetUtils.intersection(set1, set2);
console.log([...manualResult]); // Outputs: [3, 4]console.log([...utilResult]); // Outputs: [3, 4]