tags:

views:

85

answers:

2

I want to know if a list contains any value more than once. Here's what I have.

has_dupes(List) ->
    has_dupes(List, []).

has_dupes([Item|List], Seen) ->
    case lists:filter(fun(Elem) -> Elem == Item end, Seen) of
        [] ->
            has_dupes(List, [Item|Seen]);
        _ ->
            true
    end;
has_dupes([], _Seen) ->
    false.

Is there a simpler/more concise/more idiomatic way to do this? I'm pretty new at the Erlang.

+2  A: 

What about this possible solution?

has_dupes([H|T]) -> 
 case lists:member(H, T) of 
  true -> true;
  false -> has_dupes(T)
 end;
has_dupes([]) -> false. 
xboard
Yes! I must have read through the list of functions in lists 3 times and I managed to miss member. Thank you.
Nathon
+3  A: 
erlang:length(List) == sets:size(sets:from_list(List)).
mpm