unionCardinality<T> static method

int unionCardinality<T>(
  1. CustomSet<T> a,
  2. CustomSet<T> b
)

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

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

This method calculates the cardinality without actually computing the union, which can be more efficient for large sets.

Example:

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

Implementation

static int unionCardinality<T>(CustomSet<T> a, CustomSet<T> b) {
  final intersectionCardinality = SetOperations.intersection(
    a,
    b,
  ).cardinality;
  return a.cardinality + b.cardinality - intersectionCardinality;
}