Check duplicate values in array

5

My question is:

I have the following array:

$array = array(10, 30, 10, 40, 40);

I would like to know if there is a simple way to display the message: "There are duplicate values" or "There are no duplicate values".

The array_count_values() function could manipulate this array together with foreach() by comparing result to result until you find an amount greater than 1. However, I would like to get away from this analysis because I only need to know if duplicate values occur and not what are these values. Would anyone have another idea?

    
asked by anonymous 17.04.2014 / 22:51

1 answer

8

A practical way (but I do not know how to think about performance) is to filter the duplicates to another array, and compare the sizes:

$array = array(10, 30, 10, 40, 40);
$copia = array_unique($array);
if(count($copia) != count($array)) {
    echo "existem valores duplicados";
} else {
    echo "não existem valores duplicados";
}

Demo

    
17.04.2014 / 22:54