Remove Duplicates (Keep Order)
PHP
Medium
5 views
Problem Description
Remove duplicates but keep the first time each value appears.
Input Format
First n. Next line n integers.
Output Format
One line unique values.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
$seen=[]; $output=[];
for($k=0;$k<$n;$k++){
$v=$tokens[$i++] ?? '0';
if(!isset($seen[$v])){ $seen[$v]=true; $output[]=$v; }
}
echo implode(' ',$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!