How to delete duplicates in a dart List? list.distinct()?

后端 未结 11 2118
夕颜
夕颜 2020-12-01 05:21

How do i delete duplicates from a list without fooling around with a set? Is there something like list.distinct()? or list.unique()?

void main() {
  print(\"         


        
11条回答
  •  借酒劲吻你
    2020-12-01 05:31

    If you want to keep ordering or are dealing with more complex objects than primitive types. Store seen ids to the Set and filter away those ones that are already in the set.

    final list = ['a', 'a', 'b'];
    final seen = Set();
    final unique = list.where((str) => seen.add(str)).toList();
    
    print(unique); // => ['a', 'b']
    
    

提交回复
热议问题