Dart – Intersection of Sets

To find the intersection of two sets, call intersection() on the first set, and pass the second set as argument to the method. Set.intersection() method returns a new Set formed with the intersection of the two sets.

Syntax

The syntax to find the intersection of two Sets: s1, and s2 is

s1.intersection(s2)
ADVERTISEMENT

Examples

In the following program, we take two Sets: s1 and s2, and find the intersection of these two Sets using intersection() method.

main.dart

void main() {
  Set s1 = {'apple', 'banana', 'cherry'};
  Set s2 = {'mango', 'banana'};
  Set s3 = s1.intersection(s2);
  print('s1  : $s1');
  print('s2  : $s2');
  print('s1 intersection s2  : $s3');
}

Output

s1  : {apple, banana, cherry}
s2  : {mango, banana}
s1 intersection s2  : {banana}

Conclusion

In this Dart Tutorial, we learned how to find the intersection of two Sets in Dart language, with examples.