0
0
PhpProgramBeginner · 2 min read

PHP Program to Remove Duplicates from String

Use implode('', array_unique(str_split($string))) in PHP to remove duplicate characters from a string by splitting it, filtering unique characters, and joining them back.
📋

Examples

Inputhello
Outputhelo
Inputprogramming
Outputprogamin
Input
Output
🧠

How to Think About It

To remove duplicates from a string, think of the string as a list of characters. You want to keep only the first occurrence of each character and ignore the rest. So, split the string into characters, pick unique ones in order, then join them back into a string.
📐

Algorithm

1
Get the input string.
2
Split the string into an array of characters.
3
Remove duplicate characters by keeping only the first occurrence.
4
Join the unique characters back into a single string.
5
Return or print the resulting string.
💻

Code

php
<?php
$string = "programming";
$unique = implode('', array_unique(str_split($string)));
echo $unique;
?>
Output
progamin
🔍

Dry Run

Let's trace the string 'programming' through the code to remove duplicates.

1

Input string

$string = 'programming'

2

Split into characters

['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']

3

Remove duplicates

['p', 'r', 'o', 'g', 'a', 'm', 'i', 'n']

4

Join characters

'progamin'

IndexCharacterUnique Array After Step
0p['p']
1r['p', 'r']
2o['p', 'r', 'o']
3g['p', 'r', 'o', 'g']
4r['p', 'r', 'o', 'g']
5a['p', 'r', 'o', 'g', 'a']
6m['p', 'r', 'o', 'g', 'a', 'm']
7m['p', 'r', 'o', 'g', 'a', 'm']
8i['p', 'r', 'o', 'g', 'a', 'm', 'i']
9n['p', 'r', 'o', 'g', 'a', 'm', 'i', 'n']
10g['p', 'r', 'o', 'g', 'a', 'm', 'i', 'n']
💡

Why This Works

Step 1: Split string into characters

Using str_split() breaks the string into an array of single characters so we can work with each one.

Step 2: Remove duplicates

The array_unique() function keeps only the first occurrence of each character, removing duplicates.

Step 3: Join characters back

Finally, implode('') joins the unique characters back into a single string without spaces.

🔄

Alternative Approaches

Using loop and string functions
php
<?php
$string = "programming";
$result = '';
for ($i = 0; $i < strlen($string); $i++) {
    if (strpos($result, $string[$i]) === false) {
        $result .= $string[$i];
    }
}
echo $result;
?>
This method manually checks each character and builds the result string, which is easy to understand but less concise.
Using regular expressions
php
<?php
$string = "programming";
$result = preg_replace_callback('/(.)\\1+/', function($m) { return $m[1]; }, $string);
echo $result;
?>
This removes consecutive duplicates only, so it works differently and is useful if duplicates are next to each other.

Complexity: O(n) time, O(n) space

Time Complexity

Splitting the string and filtering unique characters each take linear time relative to the string length, so overall time is O(n).

Space Complexity

Extra space is needed to store the array of characters and the unique array, so space complexity is O(n).

Which Approach is Fastest?

Using array_unique with str_split is concise and efficient; manual loops are slower but more flexible.

ApproachTimeSpaceBest For
array_unique + str_splitO(n)O(n)Quick and simple duplicate removal
Manual loop with strposO(n^2)O(n)Understanding logic, small strings
Regex for consecutive duplicatesO(n)O(n)Removing only consecutive duplicates
💡
Use array_unique(str_split($string)) to quickly remove duplicate characters from a string in PHP.
⚠️
Beginners often try to remove duplicates without splitting the string into characters first, which causes errors.