30秒学会 Dart 片段 · 2019年10月22日

30秒学会 Dart 片段 – difference

Returns the difference between two lists.

Use Iterable.toSet() to get the unique values in b.
Use Iterable.where() in combination with Iterable.contains() to keep only the values in a not contained in b, Iterable.toList() to return the appropriate result.

代码实现

List<T> difference<T>(Iterable<T> a, Iterable<T> b) {
  final s = b.toSet();
  return a.where((x) => !s.contains(x)).toList();
}

使用样例

difference([1, 2, 3], [1, 2, 4]); // [3]