Search code examples
phparraysstdoutstdin

How to find count of non-unique elements of array in php using stdin for input


following will be the input on STDIN.

The first line contains an integer, n, denoting the size of the numbers array.

Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer describing the value of numbers[i].

I want to print the integer denoting the number of non-unique values in numbers to STDOUT.

Sample Input

8 1 3 1 4 5 6 3 2

Sample Output

2


Solution

  • Try using array_unique() and count() functions:

    $array = [];
    while ($in = fread(STDIN, 1024)) { 
       $array = array_merge($array, explode("\n", $in)); 
    }
    
    array_shift($array);
    
    echo (count($array) - count(array_unique($array)));