To check if two sets are disjoint in typescript, one way is to check if their intersection is an empty set. We can do this by comparing the size of the intersection set to 0.
Here's an example implementation:
index.ts259 chars8 lines
Explanation:
areSetsDisjoint
takes in two sets of type T
.intersection
by filtering out the elements in set1
that are not in set2
.intersection
set is empty by comparing its size to 0. If it is empty, the sets are disjoint and the function returns true
. Otherwise, it returns false
.We can use this function like this:
index.ts201 chars7 lines
gistlibby LogSnag