Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda