Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicate dart

there is a list:

@Koolkid3
@Peetea
@Peetea
@Ruptan
@bulunabu
@ptygma
@ptygma

Here’s what to get:

@Koolkid3
@Ruptan
@bulunabu

If there are duplicates, then you need to remove them completely. Therefore, toSet() will not work here. How to do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What about the following:

  List<String> list = [
    "@Koolkid3",
    "@Peetea",
    "@Peetea",
    "@Ruptan",
    "@bulunabu",
    "@ptygma",
    "@ptygma"
  ];
  var occurrenceCount = Map();

  list.forEach((x) => occurrenceCount[x] = !occurrenceCount.containsKey(x) ? (1) : (occurrenceCount[x] + 1));

  list.retainWhere((element) => occurrenceCount[element] == 1);
  print(list);

Use a Map to count occurrence of each item, then use retainWhere to only keep the items that occur once.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading