Finding unique elements of a list
Thomas Lindgren
thomasl_erlang@REDACTED
Thu Dec 2 11:22:42 CET 2004
Here is a straightforward version with good O(.):
first sort the list, then drop consecutive duplicates.
Complexity is given by the sort (assuming unit cost of
comparing elements). O(N log N), presumably?
unique(Xs) ->
rem_dups(lists:sort(Xs)).
rem_dups([X|Xs]) ->
[X|drop(X, Xs)];
rem_dups([]) ->
[].
drop(X, [X|Xs]) ->
drop(X, Xs);
drop(X, Ys) ->
rem_dups(Ys).
The code above seems to work, but could be optimized
further; which is left as an exercise to the reader.
Best,
Thomas
__________________________________
Do you Yahoo!?
Meet the all-new My Yahoo! - Try it today!
http://my.yahoo.com
More information about the erlang-questions
mailing list