Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

My aim is to know whether the data has same element.

Issame[str_] :=
(Do[
  If[
  str[[i, 1]] == str[[j, 1]] && str[[i, 2]] == str[[j, 2]] ,
    Return[True],
    Return[False]
],
{i, 1 , Length[str] - 1}, {j, i + 1, Length[str]}
])

Use function:

snake = {{0, 1}, {0, 0}, {1, 0}, {2, 0}, {3, 0}}
Issame[snake]
(*True*)

But the result is false factally.I want know why?

share|improve this question

marked as duplicate by Mr.Wizard Aug 2 '13 at 21:33

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.

    
What is your expected result? –  rm -rf Aug 2 '13 at 14:53
1  
Your function only checks the first two elements, then it either returns True or False. It shouldn't return anything if the first two elements don't match, it should continue. I usually avoid Do in Mathematica as much as I can and instead go for built-in functions (see Danvil's answer for example). –  Volker Aug 2 '13 at 15:11
    
@ rm -rf♦,my expected result is "False"?what's the mistake in my code? –  auto Aug 2 '13 at 15:41
1  
Since the answers to this question make it effectively a duplicate of (6745) I shall close it as such unless someone can give me a compelling reason not to. –  Mr.Wizard Aug 2 '13 at 16:54

3 Answers 3

up vote 3 down vote accepted

You can do this more elegantly like this:

Count[Tally[snake], #[[2]]>1 &] != 0

or like this:

Length[Union[snake]] != Length[snake]
share|improve this answer

To find if a list has duplicate, using DuplicateFreeQ (this command might be available only for V 9, which is what I use, can't check on older versions).

snake = {{0, 1}, {0, 0}, {1, 0}, {2, 0}, {3, 0}}
DuplicateFreeQ[snake]
(*True*)

a = {1, 2, 3, 3};
DuplicateFreeQ[a]
(* False *)

a = {1, 2, 3};
DuplicateFreeQ[a]
(* True *)

a = {{1, 1}, {1, 0}, {1, 0}};
DuplicateFreeQ[a]
(*False*)

a = {{1, 1}, {1, 0}, {1, 9}};
DuplicateFreeQ[a]
(*True*)
share|improve this answer
    
DuplicateFreeQ is not V8's function.But thank you very much! –  auto Aug 2 '13 at 15:39
    
Nasser, please add this function as an answer to the appropriate question! –  Mr.Wizard Aug 2 '13 at 16:55
Unequal @@ {1, 1, 2, 3}
(*False*)
Unequal @@ {1, 2, 3}
(*True*)

You can also use UnsameQ.

share|improve this answer

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