Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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

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

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

my solution would be:

function isDuoDigit(num) {

  const numArr = Array.from(num.toString());

  const uniqueNums = numArr.filter((num, index) => numArr.indexOf(num) === index);

  return uniqueNums.length > 2 ? "Number is not DuoDigit" : "Number is DuoDigit!"
}

console.log(isDuoDigit(11111000000)); // Number is DuoDigit!
console.log(isDuoDigit(111155555500000)); // Number is NOT DuoDigit!

  1. Convert a number to an array of strings ( 102 = ["1", "0", "2"] )

    const numArr = Array.from(num.toString());

  2. Get an array with unique values (without duplicates)

    const uniqueNums = numArr.filter((num, index) => numArr.indexOf(num) === index);

  3. We check if length of uniqueNums array is more then 2. If it's true, it means number is NOT DuoDigit and if length of array 2 or less, number is DuoDigit

    return uniqueNums.length > 2 ? "Number is not DuoDigit" : "Number is DuoDigit!"

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to do it without a set or string conversion.

function isDuoDigit(num){
    num = Math.abs(num);
    let first = '', second = '';
      while (num > 0) {
        let digit = num % 10;
        if(first === '')
          first = digit
        else if(second === '')
          second = digit
        else if(first !== digit && second !== digit)
           return false;
        num = Math.trunc(num / 10);
      }
  return true;
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda