= Solution
Keep precisely those elements with no strictly smaller element in the input. Express strictness using the <partial order> itself, avoiding reliance on Scala reference equality.
``
def minimal[T <: Ordered[T]](xs: Set[T]): Set[T] =
xs.filter { x =>
!xs.exists(y => (y <= x) && !(x <= y))
}
``
In a <partial order>, $y<x$ means $y\leq x$ and $x\not\leq y$. Therefore
$$
\boxed{\mathrm{minimal}(X)=\{x\in X:\nexists y\in X,\ y<x\}.}
$$
The empty <set> produces the empty <set>. An incomparable pair is retained in full; a <minimal element> need not be a least element. At most $O(|X|^2)$ order comparisons are needed, and only the supplied F-bounded generic interface is required.
Back to article page