Remove Duplicate Characters
PHP
Medium
5 views
Problem Description
Remove duplicate characters but keep the first occurrence (spaces included).
Input Format
One line string s.
Output Format
One line result.
Official Solution
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
$seen=[]; $out='';
for($i=0,$n=strlen($inputText);$i<$n;$i++){
$ch=$inputText[$i];
if(!isset($seen[$ch])){ $seen[$ch]=true; $out.=$ch; }
}
echo $out;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!