How to create List of common element included if there are duplicates from two different List in Dart?

I have Two List, and I hope to create a new list with elements in common and also with elements that have the same name

List<String> A=['cow','dog','cat','rat','dog'];
List<String> B=['dog','horse','rat','bird'];

I expect a result like this:

List<String> C=['dog','rat','dog'];


  • You can get your answer with below code:

    List<String> C =  A.where((e) => B.contains(e)).toList();