Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

extract common elements from lists dart

// you can use fold and set

main() {
  final lists = [
    [1, 2, 3, 55, 7, 99, 21],
    [1, 4, 7, 65, 99, 20, 21],
    [0, 2, 6, 7, 21, 99, 26]
  ];

  final commonElements =
      lists.fold<Set>(
        lists.first.toSet(), 
        (a, b) => a.intersection(b.toSet()));

  print(commonElements);
}

// gives {7, 99, 21}
 
PREVIOUS NEXT
Tagged: #extract #common #elements #lists #dart
ADD COMMENT
Topic
Name
8+6 =