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);
?>
Similar Reads
How to find Sum the Digits of a given Number in PHP ?
We will explore how to find the sum of the digits of a given number in PHP. This task involves extracting each digit from the number and adding them together to get the final sum. Table of Content Using a loop to extract digits one by oneUsing mathematical operations to extract digitsUsing a loop to
2 min read
How to convert a String into Number in PHP ?
Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are
4 min read
How to Convert a Given Number to Words in PHP?
Given an integer N, your task is to convert the given number into words using PHP.Examples:Input: N = 958237764Output: Nine Hundred Fifty Eight Million Two Hundred Thirty Seven Thousand Seven Hundred Sixty FourInput: N = 5000Output: Five ThousandBelow are the approaches to convert a given number to
5 min read
How to get the number of lines in a file using PHP?
Given a file reference, find the number of lines in this file using PHP. There are a total of 3 approaches to solve this. test.txt: This file is used for testing all the following PHP codes Geeks For Geeks Approach 1: Load the whole file into memory and then use the count() function to return the nu
2 min read
How to get total number of elements used in array in PHP ?
In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra
2 min read
How to convert an Integer Into a String in PHP ?
The PHP strval() function is used to convert an Integer Into a String in PHP. There are many other methods to convert an integer into a string. In this article, we will learn many methods.Table of ContentUsing strval() function.Using Inline variable parsing.Using Explicit Casting.Using sprintf() Fun
3 min read
PHP Program to Print ASCII Value of all Digits of a Given Number
Given a number, the task is to print ASCII value of all digits of a given number in PHP. ASCII values are often used to represent characters and digits. Sometimes, it's necessary to find the ASCII value of each digit in a given number, especially when dealing with character encoding or data manipula
3 min read
How to repeat a string to a specific number of times in PHP ?
A string is a sequence of characters stored in PHP. The string may contain special characters or numerical values or characters. The strings may contain any number of characters and may be formed by the combination of smaller substrings. Table of ContentUsing for loopUsing str_repeat methodUsing Rec
3 min read
How to extract Numbers From a String in PHP ?
Extracting numbers from a string involves identifying and isolating numerical values embedded within a text. This process can be done using programming techniques, such as regular expressions, to filter out and retrieve only the digits from the string, ignoring all other characters.Here we have some
3 min read
Counting numbers with given digits and digit sum
Given a number N, count the numbers X of length exactly N such that the number X and the sum of digits of the number X have digits A and B only in their decimal representation. The length of a number is defined as the number of digits in its decimal representation without leading zeroes. Note: As th
11 min read