distinct method
Returns a new lazy Iterable containing only distinct elements.
This method filters the current iterable, ensuring that only unique elements are included in the resulting iterable. The order of elements is preserved.
-
Returns: A new lazy Iterable containing only distinct elements.
-
Examples:
Iterable<int> numbers = [1, 2, 2, 3, 4, 4, 5]; Iterable<int> distinctNumbers = numbers.distinct(); print(distinctNumbers); // [1, 2, 3, 4, 5]
Note: This method does not modify the original iterable. It creates a new lazy iterable with distinct elements from the original iterable while preserving their order.
Implementation
Iterable<T> distinct() sync* {
var existing = HashSet<T>();
for (var current in this) {
if (existing.add(current)) {
yield current;
}
}
}