REDO
(As Pavel noted, I did not answer the question, but only turned to the unifying bat. Therefore, I went overboard for fun below.)
Here is a purely recursive single-pass method for comparison only:
-module(nondupes). -export([leave_uniques/1]). leave_uniques(L) -> lists:reverse(lu(lists:sort(L))). lu(L = [H,H|_]) -> lu(L, [], dupe); lu([H|T]) -> lu(T, [H], solo). lu([H,H], A, dupe) -> A; lu([_,H], A, dupe) -> [H|A]; lu([H,H], A, solo) -> A; lu([P,H], A, solo) -> [P,H|A]; lu([H | T = [H|_]], A, dupe) -> lu(T, A, dupe); lu([_ | T], A, dupe) -> lu(T, A, solo); lu([H | T = [H|_]], A, solo) -> lu(T, A, dupe); lu([P | T], A, solo) -> lu(T, [P|A], solo).
If past experience is some kind of judge, it is probably more effective on large lists than a choice / set of subtractions - but past experience also tells me that most of the time is not really very important. In any case, it was something interesting.
Using:
2> nondupes:leave_uniques([2, 3, 4, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9, 10, 10, 10]). [2,3,5,7]