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

后端 未结 11 2117
夕颜
夕颜 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:42

    Set works okay, but it doesn't preserve the order. Here's another way using LinkedHashSet:

    import "dart:collection";
    
    void main() {
      List arr = ["a", "a", "b", "c", "b", "d"];
      List result = LinkedHashSet.from(arr).toList();
      print(result); // => ["a", "b", "c", "d"]
    }
    

    https://api.dart.dev/stable/2.4.0/dart-collection/LinkedHashSet/LinkedHashSet.from.html

提交回复
热议问题