Why is there no List.duplicate() or List.unique()

4,738 views
Skip to first unread message

Gero

unread,
Aug 20, 2012, 5:11:32 AM8/20/12
to mi...@dartlang.org
Hi,
How can i delete duplicates in my list? Why is there no list.duplicate(); or list.unique()?


Set seems to be AWESOME fast, but it does not keep the order of the items :/

Lasse R.H. Nielsen

unread,
Aug 20, 2012, 7:30:05 AM8/20/12
to mi...@dartlang.org
On Mon, Aug 20, 2012 at 11:11 AM, Gero <geronim...@gmail.com> wrote:
> Hi,
> How can i delete duplicates in my list? Why is there no list.duplicate(); or
> list.unique()?

Removing duplicates isn't uniquely defined in a list: Which element is
retained - the first or the last (or a middle one if more than two)?

I'd write it myself to fit whatever strategy I want:

// Retains first instance of element in list.
function dedup(List list) {
Set seen = new Set();
int unique = 0;
for (int i = 0; i < list.length; i++) {
var element = list[i];
if (!seen.contains(element)) {
seen.add(element);
list[unique++] = element;
}
list.length = unique;
}
}

>
> http://stackoverflow.com/questions/12030613/how-to-delete-duplicates-in-a-dart-list-list-distinct
>
> Set seems to be AWESOME fast, but it does not keep the order of the items :/

True. There is no LinkedHashSet. You could use a LinkedHashMap
instead, and the iterate the keys, but that's more complicated.



/L
--
Lasse R.H. Nielsen
l...@google.com
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K -
Denmark - CVR nr. 28 86 69 84

Gero

unread,
Aug 20, 2012, 10:34:01 AM8/20/12
to mi...@dartlang.org
just remove the 2nd occurring already existing item (if strings or nums). What would be the problem there?

Or just use the already existing logic which is used with: set = new set.from(list) BUT keep the order of items (first occurrences) 
Reply all
Reply to author
Forward
0 new messages