duplicates property

List<T> get duplicates

Returns a new list containing only the duplicate elements from this list. Note that if an element is duplicated multiple times, it will appear multiple times in the result.

Example usage:

void main() {
  List<int> duplicates = [1, 2, 2, 3, 3, 3];
  List<int> duplicateNumbers = duplicates.duplicates;
  print(duplicateNumbers); // Output: [2, 3, 3]
}

Implementation

List<T> get duplicates => [
  for (var i = 0; i < length; i++) [...this].skip(i + 1).contains(this[i]) ? this[i] : null,
].whereType<T>().toList();