unionCardinality3<T> static method

int unionCardinality3<T>(
  1. CustomSet<T> a,
  2. CustomSet<T> b,
  3. CustomSet<T> c
)

Calculates the cardinality of the union of three sets using the Inclusion-Exclusion Principle.

Formula: |A ∪ B ∪ C| = |A| + |B| + |C| - |A ∩ B| - |A ∩ C| - |B ∩ C| + |A ∩ B ∩ C|

Example:

final a = CustomSet<int>([1, 2, 3, 4]);
final b = CustomSet<int>([3, 4, 5, 6]);
final c = CustomSet<int>([5, 6, 7, 8]);
print(CardinalityUtils.unionCardinality3(a, b, c)); // Output: 8

Implementation

static int unionCardinality3<T>(
  CustomSet<T> a,
  CustomSet<T> b,
  CustomSet<T> c,
) {
  final ab = SetOperations.intersection(a, b).cardinality;
  final ac = SetOperations.intersection(a, c).cardinality;
  final bc = SetOperations.intersection(b, c).cardinality;
  final abc = SetOperations.intersection(
    SetOperations.intersection(a, b),
    c,
  ).cardinality;

  return a.cardinality + b.cardinality + c.cardinality - ab - ac - bc + abc;
}