0

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

2
  • Question is unclear Commented Feb 15, 2017 at 7:27
  • I have not worked with STDIN and STDOUT before so dont know how to use them. I know array_unique can solve my problem but i dont know how to read data through stdin and store it in an array Commented Feb 15, 2017 at 7:37

1 Answer 1

1

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)));
Sign up to request clarification or add additional context in comments.

9 Comments

You need to remove the 8 from your array, because it is (like stated in the original problem) the length of the following integer sequence
Thanks for your support. Highly appreciated. The problem is I know hwo to do this with array_unique and count but dont know how to read the data through STDIN and store it in the $array.
use array_shift($array); to remove first element from array @FlorianSchöffl
Try something like while ($in = fread(STDIN, 1024)) { $array = array_merge($array, explode("\n", $in)); } @JatinPurswani
Thanks Mith. But this will take the first value of 8 as well in the array i guess?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.