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?
>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.