How to check if an integer include specific digits x times in TypeScript

1 Answer

0 votes
function integerIncludeDigitXTimes(n: number , xtims: number, digit: number): boolean {
    let count: number = 0;
 
    // Count occurrences of the digit in the number
    while (n > 0) {
        if (n % 10 === digit) {
            count++;
        }
        n = Math.floor(n / 10); // Perform integer division
    }
 
    // Return true if count matches xtims
    return count === xtims;
}
 
console.log(integerIncludeDigitXTimes(7097175, 3, 7)); 
console.log(integerIncludeDigitXTimes(70975, 3, 7));
 
 
 
/*
run:
 
true
false
 
*/
 

 



answered Apr 27, 2025 by avibootz
...