Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function to check if a given number do contain les than two different digits

I want to implement a function to check if a given number do contain les than two different digits, that are called duodigits

For example :

12 , 110 , -33333 : are all duodigits , since they have no more than two different digits
102 : is not a duodigit since his digits ; 1 and 0 and 2 are three different digits

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How may I implement a method , which may iterate and check if it’s a duodgits or not and return true or false as result

>Solution :

Here is how I would do it:

/**
 * Checks if a number is a duodigit.
 * @param {number} num 
 * @returns {boolean}
 */
function isDuodigit(num) {
  return new Set(Math.abs(num).toString()).size <= 2;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading