Distinct number of 2-combination in a list of sets

41 Views Asked by At

I have a collection of sets . For example [{1, 3, 4}, {1,3}, {3}, {1, 3}, {3},{}, {0, 1}, {0, 2}, {0, 2}, {2}] . I need to calculate all possible distinct number of 2-combinations in each set without repetition . For example (1,3) is repeated in the second set so I don't need to count it again . Like here I get (1,3)(1,4)(3,4)(0,1)(0,2) . So the total is 5 . I need 5 as the output of the algorithm .

1

There are 1 best solutions below

2
On

This seems only tangentially math-related but I'd do this as a start:

  1. Get rid of the empty sets and one-element sets.
  2. For every set with more than two elements, split out all of the combinations into two-element sets.
  3. Order the elements in each set so that the lower number (if it exists) is first.
  4. Sort the entire list by first number, then second number.
  5. Remove the duplicates.
  6. Count the number of elements in the list.