开发者

How can I detect if a list contains duplicates?

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开发者_如何学C.

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


erlang:length(List) == sets:size(sets:from_list(List)).


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. 


Had a 2 million element list of binaries to check. Ran these two versions for speed. usort appears to win, 6 seconds vs 316 seconds.

14> timer:tc(fun() ->  erlang:length(X) == erlang:length(lists:usort(X))  end).
{6825493,false}
15> timer:tc(fun() ->  erlang:length(X) == sets:size(sets:from_list(X)) end).  
{316297882,false}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜