@rbxts/jsnatives
difference
Creates a new Set containing elements that exist in the first set but not in the second set.
Signature
function difference<T>(set1: Set<T>, set2: Set<T>): Set<T>
Description
The difference
function creates a new Set containing only the elements that are present in the first set but not in the second set. It is sometimes referred to as the "relative complement" of the second set in the first set.
Parameters
set1
: The first Set (the set to subtract from).set2
: The second Set (the set to subtract).
Return value
- A new Set containing only the elements that exist in
set1
but not inset2
.
Examples
Basic usage
// Create two setsconst set1 = new Set([1, 2, 3, 4]);const set2 = new Set([3, 4, 5, 6]);
// Find elements in set1 that are not in set2const result = SetUtils.difference(set1, set2);console.log([...result]); // Outputs: [1, 2]
With string elements
// Sets with string elementsconst fruits1 = new Set(["apple", "banana", "orange", "kiwi"]);const fruits2 = new Set(["banana", "kiwi", "grape"]);
// Find fruits in first set that are not in second setconst uniqueFruits = SetUtils.difference(fruits1, fruits2);console.log([...uniqueFruits]); // Outputs: ["apple", "orange"]
Order matters
// Difference is not commutative - order mattersconst set1 = new Set([1, 2, 3]);const set2 = new Set([2, 3, 4]);
// set1 - set2const result1 = SetUtils.difference(set1, set2);console.log([...result1]); // Outputs: [1]
// set2 - set1const result2 = SetUtils.difference(set2, set1);console.log([...result2]); // Outputs: [4]
Empty and identical sets
// Difference with an empty setconst numbers = new Set([1, 2, 3]);const empty = new Set();
// set - empty = setconst result1 = SetUtils.difference(numbers, empty);console.log([...result1]); // Outputs: [1, 2, 3]
// empty - set = emptyconst result2 = SetUtils.difference(empty, numbers);console.log([...result2]); // Outputs: []
// Difference with identical setsconst set = new Set([1, 2, 3]);const sameset = new Set([1, 2, 3]);const result3 = SetUtils.difference(set, sameset);console.log([...result3]); // Outputs: []
Practical application
// Remove banned users from active users listfunction removeInactiveMemberships(allUsers: Set<string>, inactiveUsers: Set<string>): Set<string> { return SetUtils.difference(allUsers, inactiveUsers);}
// All usersconst allUsers = new Set([ "user1", "user2", "user3", "user4", "user5"]);
// Inactive users to be removedconst inactiveUsers = new Set([ "user2", "user5"]);
// Get active users onlyconst activeUsers = removeInactiveMemberships(allUsers, inactiveUsers);console.log([...activeUsers]);// Outputs: ["user1", "user3", "user4"]
Filtering items
// Filter out items that match certain criteriafunction filterSelectedItems<T>(allItems: Set<T>, excludeItems: Set<T>): Set<T> { return SetUtils.difference(allItems, excludeItems);}
// Shopping cart itemsconst cartItems = new Set(["shirt", "pants", "hat", "shoes", "socks"]);
// Items on backorder (unavailable)const backorderedItems = new Set(["hat", "shoes"]);
// Get only available itemsconst availableItems = filterSelectedItems(cartItems, backorderedItems);console.log([...availableItems]);// Outputs: ["shirt", "pants", "socks"]
Comparison with manual implementation
// Manual implementation of difference operationfunction manualDifference<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.differenceconst set1 = new Set([1, 2, 3, 4, 5]);const set2 = new Set([4, 5, 6, 7]);
const manualResult = manualDifference(set1, set2);const utilResult = SetUtils.difference(set1, set2);
console.log([...manualResult]); // Outputs: [1, 2, 3]console.log([...utilResult]); // Outputs: [1, 2, 3]