Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Array (list) Elements Unique in Dart?

I need to remove redundant (String) elements from a list. Or perhaps to prevent them from being entered in the first place is the better solution? Sets do not allow duplicates, but they also do not keep order and I need the order. This is a common problem for me so I am looking for a possible language solution for full efficiency.

(In the past I have extended an Array Class to add my own add_unique() method, but this seems like it is a common enough issue to be handled by the language and likely more efficiently.)

Thanks,

_g

like image 668
george koller Avatar asked Oct 15 '12 21:10

george koller


1 Answers

Use toset and then toList

 var ids = [1, 4, 4, 4, 5, 6, 6];
 var distinctIds = ids.toSet().toList();
like image 199
SilenceCodder Avatar answered Oct 16 '22 07:10

SilenceCodder