PHP Program to find duplicate characters in a string


February 15, 2022, Learn eTutorial
2038

What is meant by the duplicate characters in a string?

This program is to find the repeating characters in the entered string. For example, if 'Hello World' is the entered string then the duplicate characters will be 'l' and 'o'.

How to find duplicate characters in a string using PHP?

In this program, we are using the static string and it is assigned to the variable string. Then we have to convert the string into lowercase using the built-in function strtolower() and find the length of the string and assign it to the variable size. After that assign the value 0 into the variable i and perform the operations until the condition 'i < size' becomes false and increment the value of the variable i in every iteration first assign the value 1 into the variable count and then assign the value 'i + 1' into the variable j and perform the operations until the condition 'j < size' becomes false and increment the value of the variable j in every iteration first check the conditions 'string[i] == string[j]' and 'string[i] != "" ' if both are true increment the value of variable count by 1 and assign the value 0 to the variable string[j] to avoid duplication. And at last check the condition 'count > 1' and 'string[i] != 0' if true then print the value of string[i].

ALGORITHM

Step 1: Assign the sting into the variable string

Step 2: Covert the string in the variable string to lowercase and assign it to the variable string

Step 3: Assign the length of the variable string into the variable size

Step 4: Assign the value 0 into the variable i and perform the sub-steps until the condition 'i < size' becomes false and increment the value of the variable i in every iteration

        (i) Assign the value 1 into the variable count

        (ii) Assign the value 'i + 1' into the variable j and perform the sub-steps until the condition 'j < size' becomes false and increment the value of the                   variable j in every iteration

            (a) Check the conditions 'string[i] == string[j]' and 'string[i] != "" ' if both are true increment the value of variable count by 1 and assign the                          value 0 to the variable string[j] to avoid duplication

        (iii) Check the condections 'count > 1' and 'string[i] != 0' if true then print the value of string[i]

PHP Source Code

                                          <?php
$string = "learnetutorials.com";
$string = strtolower($string);
$size = strlen($string);
echo "The entered string is : $string \n";
echo "The duplicate characters in the string are: \n";
for ($i = 0; $i < $size; $i++) {
    $count = 1;
    for ($j = $i + 1; $j < $size; $j++) {
        if ($string[$i] == $string[$j] && $string[$i] != ' ') {
            $count++;
            $string[$j] = '0';
        }
    }
    if ($count > 1 && $string[$i] != '0') {
        echo "$string[$i] \n";
    }
}
?>
                                      

OUTPUT

The entered string is : learnetutorials.com
The duplicate characters in the string are:
l
e
a
r
t
o