Finding unique elements of a list
Vance Shipley
vances@REDACTED
Wed Dec 1 21:30:02 CET 2004
On Wed, Dec 01, 2004 at 06:10:23PM +0100, Ulf Wiger (AL/EAB) wrote:
}
}
} Here's another one, shorter, O(1), and, most importantly,
} cuter (functional purists may retch at will):
}
} u2(L) ->
} T = ets:new(temp,[set]),
} L1 = lists:filter(fun(X) -> ets:insert_new(T, {X,1}) end, L),
} ets:delete(T),
} L1.
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).
-Vance
More information about the erlang-questions
mailing list