Created
January 3, 2019 18:02
-
-
Save flymio/9dbfda905f197c36e7053e99b69e512c to your computer and use it in GitHub Desktop.
solution for https://www.hackerrank.com/challenges/sock-merchant/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Complete the sockMerchant function below. | |
function sockMerchant($n, $ar) { | |
$arnew = array(); | |
$cnt=0; | |
foreach($ar as $val){ | |
if (isset($arnew[$val])){ | |
$cnt++; | |
unset($arnew[$val]); | |
} | |
else{ | |
$arnew[$val]=1; | |
} | |
} | |
return $cnt; | |
} | |
$fptr = fopen(getenv("OUTPUT_PATH"), "w"); | |
$stdin = fopen("php://stdin", "r"); | |
fscanf($stdin, "%d\n", $n); | |
fscanf($stdin, "%[^\n]", $ar_temp); | |
$ar = array_map('intval', preg_split('/ /', $ar_temp, -1, PREG_SPLIT_NO_EMPTY)); | |
$result = sockMerchant($n, $ar); | |
fwrite($fptr, $result . "\n"); | |
fclose($stdin); | |
fclose($fptr); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How??? does this work? It passes all tests!