How to Count of Repeating Digits in a Given Number in PHP ?
Last Updated :
16 Jul, 2024
Counting the number of repeating digits in a given number is a common task in programming. This can be useful in various scenarios, such as data analysis or validation. In this article, we will explore different approaches to counting the repeating digits in a given number using PHP.
1. Using String Manipulation and Arrays
One way to count repeating digits is by converting the number to a string, splitting it into an array of digits, and then use array_count_values()
function to count the occurrences of each digit. Finally, the array_filter()
function is used to filter out digits that occur only once, leaving only the repeating digits.
Example: Illustration of counting the number of repeating digits in a given number in PHP using string manipulation and arrays.
PHP
<?php
function countRepeatingDigits($number) {
// Converting the number into a string
// and splitting this string into an array of digits
$digits = str_split((string)$number);
// Counting the occurrences of each digit
$counts = array_count_values($digits);
// Filtering out counts where the digit occurs only once
// to find the repeating digits only
$repeatingCounts = array_filter($counts, function($count) {
return $count > 1;
});
return $repeatingCounts;
}
$num = 1234567654321;
$repeatingCounts = countRepeatingDigits($num);
// Iterating through the results and printing them
foreach ($repeatingCounts as $digit => $count) {
echo "Digit $digit repeats $count times\n";
}
?>
OutputDigit 1 repeats 2 times
Digit 2 repeats 2 times
Digit 3 repeats 2 times
Digit 4 repeats 2 times
Digit 5 repeats 2 times
Digit 6 repeats 2 times
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Using Arithmetic Operations
Another approach is to use arithmetic operations to extract each digit of the number and count their occurrences. Finally, filter out those digits that occur only once, leaving only the repeating digits.
Example: Illustration of counting the number of repeating digits in a given number in PHP using arithmetic operations.
PHP
<?php
function countRepeatingDigits($number) {
$counts = [];
// Loop through each digit in the given number
while ($number > 0) {
// Extract the last digit
$digit = $number % 10;
// Remove the last digit from the number
$number = (int)($number / 10);
// If the digit is not in the counts array
// initialize its count to 0
if (!isset($counts[$digit])) {
$counts[$digit] = 0;
}
$counts[$digit]++;
}
// Filter out counts where the digit occurs only once
$repeatingCounts = array_filter($counts, function($count) {
return $count > 1;
});
return $repeatingCounts;
}
$num = 1234567654321;
$repeatingCounts = countRepeatingDigits($num);
foreach ($repeatingCounts as $digit => $count) {
echo "Digit $digit repeats $count times\n";
}
?>
OutputDigit 1 repeats 2 times
Digit 2 repeats 2 times
Digit 3 repeats 2 times
Digit 4 repeats 2 times
Digit 5 repeats 2 times
Digit 6 repeats 2 times
Time Complexity: O(1)
Auxiliary Space: O(1)
3. Using strtr Function
In this approach, we will convert the number to a string and use the strtr function to replace each digit with a corresponding character that will help us track the counts. Finally, we will filter out the digits that occur only once.
Example: Illustration of counting the number of repeating digits in a given number in PHP using strtr function.
PHP
<?php
function countRepeatingDigits($number) {
$counts = [];
// Loop through each digit in the given number
while ($number > 0) {
// Extract the last digit
$digit = $number % 10;
// Remove the last digit from the number
$number = (int)($number / 10);
// If the digit is not in the counts array
// initialize its count to 0
if (!isset($counts[$digit])) {
$counts[$digit] = 0;
}
$counts[$digit]++;
}
// Filter out counts where the digit occurs only once
$repeatingCounts = array_filter($counts, function($count) {
return $count > 1;
});
return $repeatingCounts;
}
$num = 1234567654321;
$repeatingCounts = countRepeatingDigits($num);
foreach ($repeatingCounts as $digit => $count) {
echo "Digit $digit repeats $count times\n";
}
?>
OutputDigit 1 repeats 2 times
Digit 2 repeats 2 times
Digit 3 repeats 2 times
Digit 4 repeats 2 times
Digit 5 repeats 2 times
Digit 6 repeats 2 times
Using Regular Expressions
Another approach to count repeating digits in a given number is by using regular expressions. This method involves converting the number to a string and using a regular expression to find and count repeating digits. The preg_match_all function can be used to find all occurrences of each digit and determine if they are repeated.
Example: The following example demonstrates how to count repeating digits in a given number using regular expressions in PHP.
PHP
<?php
function countRepeatingDigits($number) {
// Convert the number to a string
$numberStr = strval($number);
// Array to store the count of repeating digits
$repeatingDigits = [];
// Iterate over each digit from 0 to 9
for ($i = 0; $i <= 9; $i++) {
// Use regular expression to count occurrences of each digit
preg_match_all("/$i/", $numberStr, $matches);
// Check if the count is more than 1, indicating a repeating digit
if (count($matches[0]) > 1) {
$repeatingDigits[$i] = count($matches[0]);
}
}
return $repeatingDigits;
}
$number = 1111111;
$repeatingDigits = countRepeatingDigits($number);
print_r($repeatingDigits);
?>