Skip to main content
slight addition
Source Link
jVincent
  • 14.8k
  • 1
  • 44
  • 76

If there are repeated elements in the list, then calling Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[Union[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

Note, I'm using Union rather then DeleteDuplicates[] since (as Mr. Wizard corrected me) it is faster. I can't say why except that DeleteDuplicates[] retains the order of elements which may require slightly more bookkeeping. And in this case we don't care about the book keeping. Naturally if you really needed something really speedy, a better solution exists which doesn't search through the entire list, but stops if just a single duplicate is found, Mr. Wizards Answer is just such an function, since Signature exits early if duplicates exist, though it becomes slower if no duplicates are present, it's a trade off.

If there are repeated elements in the list, then calling Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[Union[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

Note, I'm using Union rather then DeleteDuplicates[] since (as Mr. Wizard corrected me) it is faster. I can't say why except that DeleteDuplicates[] retains the order of elements which may require slightly more bookkeeping. And in this case we don't care about the book keeping. Naturally if you really needed something really speedy, a better solution exists which doesn't search through the entire list, but stops if just a single duplicate is found.

If there are repeated elements in the list, then calling Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[Union[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

Note, I'm using Union rather then DeleteDuplicates[] since (as Mr. Wizard corrected me) it is faster. I can't say why except that DeleteDuplicates[] retains the order of elements which may require slightly more bookkeeping. And in this case we don't care about the book keeping. Naturally if you really needed something really speedy, a better solution exists which doesn't search through the entire list, but stops if just a single duplicate is found, Mr. Wizards Answer is just such an function, since Signature exits early if duplicates exist, though it becomes slower if no duplicates are present, it's a trade off.

Switching back to union
Source Link
jVincent
  • 14.8k
  • 1
  • 44
  • 76

If there are repeated elements in the list, then calling DeleteDuplicates[]Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[DeleteDuplicates[list]]Length[Union[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

Note, I'm using Union rather then DeleteDuplicates[] since (as Mr. Wizard corrected me) it is faster. I can't say why except that DeleteDuplicates[] retains the order of elements which may require slightly more bookkeeping. And in this case we don't care about the book keeping. Naturally if you really needed something really speedy, a better solution exists which doesn't search through the entire list, but stops if just a single duplicate is found.

If there are repeated elements in the list, then calling DeleteDuplicates[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[DeleteDuplicates[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

If there are repeated elements in the list, then calling Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[Union[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

Note, I'm using Union rather then DeleteDuplicates[] since (as Mr. Wizard corrected me) it is faster. I can't say why except that DeleteDuplicates[] retains the order of elements which may require slightly more bookkeeping. And in this case we don't care about the book keeping. Naturally if you really needed something really speedy, a better solution exists which doesn't search through the entire list, but stops if just a single duplicate is found.

corrected function name in text
Source Link
jVincent
  • 14.8k
  • 1
  • 44
  • 76

If there are repeated elements in the list, then calling Union[]DeleteDuplicates[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[Union[list]]Length[DeleteDuplicates[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

If there are repeated elements in the list, then calling Union[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[Union[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]

If there are repeated elements in the list, then calling DeleteDuplicates[] on it will shorten it so that this element only appears once, so a simple implementation would be to test these lengths:

 test[list_] := Length[DeleteDuplicates[list]] != Length[list]

If you wanted to know which elements where repeated, you this could be accomplished by using Gather[] to collect identical elements, and picking out which groups have more then one element.

 repeats[list_] := Select[Gather[list], Length[#] > 1 &][[1 ;;, 1]]
added 7 characters in body
Source Link
Loading
added extra function
Source Link
jVincent
  • 14.8k
  • 1
  • 44
  • 76
Loading
Source Link
jVincent
  • 14.8k
  • 1
  • 44
  • 76
Loading