Remove Duplicates (Keep Order)

Remove Duplicates (Keep Order)

Medium PHP PHP Arrays 30 views
Explanation Complexity

Problem Statement

Remove duplicates but keep the first time each value appears.

Input Format

First n. Next line n integers.

Output Format

One line unique values.

Example

8
1 2 1 3 2 4 4 5
1 2 3 4 5

Constraints

n

Input / Output Format

Input Format
First n. Next line n integers.
Output Format
One line unique values.
Constraints
n

Examples

Input:
8 1 2 1 3 2 4 4 5
Output:
1 2 3 4 5

Example Solution (Public)

PHP
<?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);
?>

Official Solution Code

<?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);
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.