PHP Program to Print All Duplicate Characters in a String
This article will show you how to print duplicate characters in a string using PHP. Detecting and printing duplicate characters in a string is a common task in programming. In this article, we will explore various approaches to achieve this.
Table of Content
Using array_count_values() Function
The array_count_values() function is used to count the occurrences of each value in an array, simplifying the process.
<?php
function printDuplicates($str) {
$charCount = array_count_values(str_split($str));
// Print duplicate characters
echo "Duplicate characters: ";
foreach ($charCount as $char => $count) {
if ($count > 1) {
echo "$char ";
}
}
}
// Driver code
$str = "Welcome to GeeksforGeeks";
printDuplicates($str);
?>
<?php
function printDuplicates($str) {
$charCount = array_count_values(str_split($str));
// Print duplicate characters
echo "Duplicate characters: ";
foreach ($charCount as $char => $count) {
if ($count > 1) {
echo "$char ";
}
}
}
// Driver code
$str = "Welcome to GeeksforGeeks";
printDuplicates($str);
?>
Output
Duplicate characters: e o G k s
Using Associative Array
One of the most basic methods is to use an associative array to keep track of the occurrence of each character in the string.
<?php
function printDuplicates($str) {
$charCount = [];
// Iterate through each character
// in the string
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
// Check if the character is already
// in the associative array
if (isset($charCount[$char])) {
$charCount[$char]++;
} else {
$charCount[$char] = 1;
}
}
// Print duplicate characters
echo "Duplicate characters: ";
foreach ($charCount as $char => $count) {
if ($count > 1) {
echo "$char ";
}
}
}
// Driver code
$str = "Welcome to GeeksforGeeks";
printDuplicates($str);
?>
<?php
function printDuplicates($str) {
$charCount = [];
// Iterate through each character
// in the string
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
// Check if the character is already
// in the associative array
if (isset($charCount[$char])) {
$charCount[$char]++;
} else {
$charCount[$char] = 1;
}
}
// Print duplicate characters
echo "Duplicate characters: ";
foreach ($charCount as $char => $count) {
if ($count > 1) {
echo "$char ";
}
}
}
// Driver code
$str = "Welcome to GeeksforGeeks";
printDuplicates($str);
?>
Output
Duplicate characters: e o G k s
Using a Regular Expression and preg_match_all()
Another approach to print duplicate characters in a string is by using a regular expression in combination with the preg_match_all() function. This method is particularly useful for efficiently identifying and handling characters within the string.
Example: This approach offers a better way to detect and print duplicate characters in a string, especially when dealing with Unicode characters or requiring efficient pattern matching.
<?php
function printDuplicateCharacters($inputString) {
// Initialize an empty array to store character counts
$charCounts = [];
// Use preg_match_all to get all characters
preg_match_all('/./u', $inputString, $matches);
// Iterate through each character and count occurrences
foreach ($matches[0] as $char) {
if (isset($charCounts[$char])) {
$charCounts[$char]++;
} else {
$charCounts[$char] = 1;
}
}
// Print characters that appear more than once
foreach ($charCounts as $char => $count) {
if ($count > 1) {
echo "Character '$char' appears $count times.\n";
}
}
}
// Sample usage
$inputString = "programming";
printDuplicateCharacters($inputString);
?>
<?php
function printDuplicateCharacters($inputString) {
// Initialize an empty array to store character counts
$charCounts = [];
// Use preg_match_all to get all characters
preg_match_all('/./u', $inputString, $matches);
// Iterate through each character and count occurrences
foreach ($matches[0] as $char) {
if (isset($charCounts[$char])) {
$charCounts[$char]++;
} else {
$charCounts[$char] = 1;
}
}
// Print characters that appear more than once
foreach ($charCounts as $char => $count) {
if ($count > 1) {
echo "Character '$char' appears $count times.\n";
}
}
}
// Sample usage
$inputString = "programming";
printDuplicateCharacters($inputString);
?>
Output
Character 'r' appears 2 times. Character 'g' appears 2 times. Character 'm' appears 2 times.