0
$\begingroup$

I have a list of integer, and I want to know if the list contains any duplicates. My code is working, but I feel there is a more straightforward way of doing this

list1 = Range[10];
list2 = {1,2,3,4,1,6};
DoublesQ[list_] := Count[Tally[list], {_,n_}  /; n >1 ] == 0]

DoublesQ[list1]
=> True

DoublesQ[list2]
=> False

EDIT: So appears that Mathematica 10 has a function for that. I guess that now my question is: how can I do that without the build-in function?

$\endgroup$
6
  • 2
    $\begingroup$ Length@Union@list1==Length@list1 for xample $\endgroup$ Commented Nov 5, 2014 at 17:10
  • 2
    $\begingroup$ In V10 there is DuplicateFreeQ. $\endgroup$ Commented Nov 5, 2014 at 17:14
  • $\begingroup$ @belisarius you should post an answer $\endgroup$
    – mete
    Commented Nov 5, 2014 at 17:31
  • 1
    $\begingroup$ @mete I think this one is a dup. There was a question with some performance figures for different methods, but I can't find it now. Someone will ... $\endgroup$ Commented Nov 5, 2014 at 17:36
  • $\begingroup$ I read this, but it seemed different from what I wanted: mathematica.stackexchange.com/questions/19357/… $\endgroup$
    – mete
    Commented Nov 5, 2014 at 17:45

1 Answer 1

4
$\begingroup$

New in Mathematica 10 is the function DuplicateFreeQ

list1 = Range[10];
list2 = {1,2,3,4,1,6};

DuplicateFreeQ /@ {list1, list2}
(* {True, False} *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.