I'm trying to find a solution to a problem like the following, where I have three sets (technically arrays, but they will always be guaranteed not to have repeating elements and they will always have their elements in increasing order), and I need to determine the first set of numbers that will contain exactly one element from each set and have no overlapping values itself (if such a set of numbers can exist given the group of sets):
const a = [1, 2, 3];
const b = [1, 2];
const c = [1, 2];
// In this case, the first eligible set would be [3, 1, 2].
// Order matters, so a return of [3, 1, 2] would indicate that a: 3, b: 1, and c: 2.
findFirstCoalescingSetAmongGroupOfSets([a, b, c]);
const d = [1, 2];
// In this case, there would be no eligible set.
findFirstCoalescingSetAmongGroupOfSets([a, b, c, d]);
Before I jump into a solution that I suspect will have to be recursive and tax the ol' noodle considerably, I wanted to see if javascript had a built-in function to determine this sort of thing or if there was a straightforward approach that I'm missing. I've had no luck in my investigation of Set
on MDN.
The solution will need to work for an arbitrary number of sets for which I'm seeking to find this "coalescing set".
Following @Petr suggestion I took one of possible (Kuhn) algorithm from here.