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

87
Vistas
Search for special Numbers in an array using Javascript

Write a method that finds efficiently with respect to time used, all numbers that occur exactly once in the input array. Return an empty array if no such numbers are found. for example, printSpecialNumbers([1,5,1,7]) should return [5, 7];

function printSpecialNumbers(niqueNum) {
}

let result = printSpecialNumbers([1,5,1,7]);
console.log(result); 

Apparently my code only prints one of the special numbers being five.

function printSpecialNumbers(niqueNum) {
    var obj = {};
    for (var i = 0; i<niqueNum.length; i++) {
        if (typeof obj[niqueNum[i]] !== "undefined") {
            delete obj[niqueNum[i]];
            continue;
        }
    obj[niqueNum[i]] = i;
    }
    return Number(Object.keys(obj)[0]);
}

let result = printSpecialNumbers([1, 5, 1, 7]);
console.log(result);
almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Your issue is that you're only ever returning the first value encountered by using

return Number(Object.keys(obj)[0]);

Instead, you want to return the entire array, which you can do with:

return Object.keys(obj).map(Number);

You also have another issue, which is that your code will return any number that is see an odd number of times, so (for example)

printSpecialNumbers([1, 5, 1, 7, 1])

will return [1, 5, 7]. You can work around this by maintaining a count of the number of times a number is seen and returning only those which are seen once.

function printSpecialNumbers(niqueNum) {
  var obj = {};
  for (var i = 0; i < niqueNum.length; i++) {
    obj[niqueNum[i]] = (obj[niqueNum[i]] || 0) + 1;
  }
  let res = [];
  Object.keys(obj).forEach(k => {
    if (obj[k] == 1) res.push(Number(k));
  });
  return res;
}

let result = printSpecialNumbers([1, 5, 1, 7]);
console.log(result);

almost 4 years ago · Santiago Trujillo Denunciar

0

There are probably multiple ways. Here is a quick version using the indexOf() and lastIndexOf() array methods. Basically, while the loop runs, if the index of the item being processed is equal to its last index, we know there is only one copy in the array.

function printSpecial(arr) {
    let specials = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr.indexOf(arr[i]) === arr.lastIndexOf(arr[i])) {
            specials.push(arr[i])
        }
    }
    return specials
}

let result = printSpecial([1, 5, 1, 7]);
console.log(result)

almost 4 years ago · Santiago Trujillo Denunciar

0

const printSpecialNumbers = (numbers = []) => numbers.reduce((acc,
  number, idx, all) => {
  if (all.filter(s => s === number).length < 2) {
    acc = [...acc, number]
  }
  return acc
}, [])
almost 4 years ago · Santiago Trujillo 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