-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove duplicate characters from a string
- Loading branch information
1 parent
de42d6a
commit f406fa3
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* The function loops through each character in the input string. | ||
* It uses an array $seen to keep track of characters that have already been added to the output string. | ||
* If a character hasn't been seen, it is appended to the result string, and the character is marked as seen. | ||
* The function returns the modified string with duplicate characters removed. | ||
*/ | ||
|
||
function removeDuplicatedCharacters($inputString) { | ||
// 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)) { | ||
// Add the character to the result and mark it as seen | ||
$result .= $char; | ||
$seen[] = $char; | ||
} | ||
} | ||
|
||
return $result; | ||
} |