-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathRemoveDuplicateCharacters.php
More file actions
34 lines (26 loc) · 932 Bytes
/
Copy pathRemoveDuplicateCharacters.php
File metadata and controls
34 lines (26 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
declare(strict_types=1);
/**
* Removes duplicate characters from a string, retaining only the first occurrence of each character.
*
* @param string $inputString The input string from which duplicates will be removed.
* @return string The modified string with duplicate characters removed.
*/
function removeDuplicateCharacters(string $inputString): string
{
// Initialize an empty array to keep track of seen characters
$seen = [];
// Initialize an empty string for the result
$result = '';
// Loop through each character in the input string
for ($i = 0; $i < strlen($inputString); $i++) {
$char = $inputString[$i];
// Check if the character has already been seen
if (!in_array($char, $seen, true)) {
// Add the character to the result and mark it as seen
$result .= $char;
$seen[] = $char;
}
}
return $result;
}