0
$\begingroup$

This question already has an answer here:

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$

marked as duplicate by Mr.Wizard Jul 6 '15 at 17:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ Length@Union@list1==Length@list1 for xample $\endgroup$ – Dr. belisarius Nov 5 '14 at 17:10
  • 2
    $\begingroup$ In V10 there is DuplicateFreeQ. $\endgroup$ – Leonid Shifrin Nov 5 '14 at 17:14
  • $\begingroup$ @belisarius you should post an answer $\endgroup$ – mete Nov 5 '14 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$ – Dr. belisarius Nov 5 '14 at 17:36
  • $\begingroup$ I read this, but it seemed different from what I wanted: mathematica.stackexchange.com/questions/19357/… $\endgroup$ – mete Nov 5 '14 at 17:45
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.