Forum Moderators: coopster

Message Too Old, No Replies

Tell if an array has duplicate values or not

         

dombili

11:51 am on Feb 13, 2010 (gmt 0)

10+ Year Member



I simply want to generate an array of random numbers and I also want to have no more than one instance of any numbers.

What I am thinking is having a a loop to fill in the array with random numbers and then checking the elements to see if there are any duplicates and if there are any, generating a new set.

My search led me to
array_count_values()
. It looks like I can use it and check if any of the result array elements has a value bigger than 1.

Is there a simpler way or a function for this check? (I just need to know if there are duplicates in an array or not, no removes, no replaces and such)

Thanks in advance

Readie

5:55 pm on Feb 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[uk.php.net...]
[uk.php.net...]
The above links may be what you want :)

I figured with the second one you could have a loop - comparing the previous original array with a new array (old array + new value) - if a count of both is identicle you would of had a duplicate value.

The first one will just kill off any duplicate values.

dombili

12:14 pm on Feb 14, 2010 (gmt 0)

10+ Year Member



Thanks.

Yeah, looks like there are only work-arounds like that. I just wanted to make sure if I was missing a really basic function for it.