Finding unique elements of a list

Ulf Wiger (AL/EAB) <>
Thu Dec 2 11:34:34 CET 2004


> From: Vance Shipley [mailto:]
> Those that retched may prefer this:  :)
> 
> u2(L) ->
> 	u2(L, [], gb_sets:empty()).
> u2([H|T], Acc, S) ->
> 	case catch gb_sets:insert(H, S) of
> 		{'EXIT', _} ->
> 			u2(T, Acc, S);
> 		N ->
> 			u2(T, [H|Acc], N);
> 	end;
> u2([], Acc, _S) ->
> 	lists:reverse(Acc).


This has the desireable property that the elements
of the list are not duplicated (since inserting them
in a heap-based structure only means copying the
pointer. My ets-based version would duplicate all 
elements in the final list, which could of course be
very costly if the elements are large.

/Uffe



More information about the erlang-questions mailing list