# Why is my funtion is wrong? [duplicate]

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?

-

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

What is your expected result? –  The Toad Aug 2 '13 at 14:53
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
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

You can do this more elegantly like this:

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


or like this:

Length[Union[snake]] != Length[snake]

-

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*)

-
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.

-