-
-
Notifications
You must be signed in to change notification settings - Fork 500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Strings/RemoveDuplicatedCharacters.php #180
Open
khouloudhaddad
wants to merge
9
commits into
TheAlgorithms:master
Choose a base branch
from
khouloudhaddad:khouloudHaddad
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f406fa3
Remove duplicate characters from a string
khouloudhaddad 37d62f9
Make "removeDuplicatedCharacters" PSR-12 friendly and add unit tests
khouloudhaddad a310c88
Update Strings/RemoveDuplicatedCharacters.php
khouloudhaddad 584704a
Apply fixes
khouloudhaddad 01256ea
Merge branch 'khouloudHaddad' of https://github.com/khouloudhaddad/PH…
khouloudhaddad d519dbe
Merge branch 'TheAlgorithms:master' into khouloudHaddad
khouloudhaddad 9af9243
Add RemoveDuplicateCharacters to the Directory.
khouloudhaddad 6d7d19b
Update tests/Strings/RemoveDuplicateCharactersTest.php
khouloudhaddad c283a45
Remove extra line at the end of the file
khouloudhaddad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,34 @@ | ||
<?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; | ||
} | ||
|
||
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 |
---|---|---|
|
@@ -20,4 +20,3 @@ | |
"test": "vendor/bin/phpunit tests" | ||
} | ||
} | ||
|
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
require_once __DIR__ . '/../../Strings/RemoveDuplicateCharacters.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for the removeDuplicateCharacters function. | ||
* To run test: ./vendor/bin/phpunit tests/strings/removeDuplicateCharactersTest.php | ||
*/ | ||
final class RemoveDuplicateCharactersTest extends TestCase | ||
{ | ||
/** | ||
* Test with a string that has multiple duplicates. | ||
*/ | ||
public function testStringWithDuplicates(): void | ||
{ | ||
$this->assertSame('progamin', removeDuplicateCharacters('programming')); | ||
} | ||
|
||
/** | ||
* Test with a string that has no duplicates. | ||
*/ | ||
public function testStringWithoutDuplicates(): void | ||
{ | ||
$this->assertSame('abc', removeDuplicateCharacters('abc')); | ||
} | ||
|
||
/** | ||
* Test with an empty string. | ||
*/ | ||
public function testEmptyString(): void | ||
{ | ||
$this->assertSame('', removeDuplicateCharacters('')); | ||
} | ||
|
||
/** | ||
* Test with a string containing only one character repeated. | ||
*/ | ||
public function testSingleCharacterRepeated(): void | ||
{ | ||
$this->assertSame('a', removeDuplicateCharacters('aaaaa')); | ||
} | ||
|
||
/** | ||
* Test with special characters. | ||
*/ | ||
public function testStringWithSpecialCharacters(): void | ||
{ | ||
$this->assertSame('ab!@', removeDuplicateCharacters('aabb!!@@')); | ||
} | ||
|
||
/** | ||
* Test with a string containing spaces. | ||
*/ | ||
public function testStringWithSpaces(): void | ||
{ | ||
$this->assertSame('a b', removeDuplicateCharacters('a a a b b')); | ||
} | ||
|
||
/** | ||
* Test with a string containing mixed case characters. | ||
*/ | ||
public function testStringWithMixedCase(): void | ||
{ | ||
$this->assertSame('aAbB', removeDuplicateCharacters('aAaAaAbBbB')); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file needs 1 empty new line at the end of the file. The other file needs just 1 but has two. 👍🏻 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linter wants you to remove the extra new line at the end of this file.