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

215
Vistas
Find unique number in unsorted array JavaScript

I have to find a unique number in unsorted array, but my function returns wrong number, I can't understand why. Here is my code:

function findUniq(arr) {
  let sorted = [...arr].sort()
  if(sorted.length === 0) return 0
  // do magic
  let num = 0
  for(let i = 1; i < sorted.length; i++){
    if(sorted[num] !== sorted[i]){
      num++;
      sorted[num] = sorted[i]
     
    } 
  }
  return num + 1 
}

if I invoke findUniq([9,7,7,6,6,5,5,5]) it gives 4. What do I do wrong? Thanks in advance. I forgot to mention I have to have just one for loop to implement O(n) time complexity

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

0

This should work:

It returns only one unique number in array or undefined

If you want all unique numbers replace return arr[0] with return arr. It will then return array of all unique numbers (or empty array if there are not any)

function findUniq(arr) {
    arr.filter((item, index) => {
        arr.splice(index, 1)
        const unique = !arr.includes(item)
        arr.splice(index, 0, item)
        return unique
    })

    return arr[0]
}

ES6 aproach:

function findUniq(arr) {
    return arr
        .map((c) => arr.filter((b) => c == b))
        .filter((e) => e.length < 2)
        .reduce((total, cur) => total.concat(cur), [])
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use reduce and can find the item that repeats once in the last index.

var arr = [9,7,7,6,6,5,5,5]
var uniqueNumber;
arr.reduce((obj,val,index) => 
{
    obj[val] ? ++obj[val] : obj[val] = 1;
    if(index == (arr.length - 1))
    {
      uniqueNumber = Object.keys(obj).find(key => obj[key] === 1)
    }
    return obj
},{})
console.log(uniqueNumber)

about 4 years ago · Juan Pablo Isaza Denunciar

0

To do it in a single O(n) loop, reduce, keeping track of counts as well as a set of singular items.

function findUniq(arr) {
    let [single] = arr.reduce((acc, el) => {
      if (!acc[el]) acc[el] = 0;
      if (++acc[el] === 1) acc.singular.add(el);
      else acc.singular.delete(el);
      return acc;
    }, { singular: new Set() }).singular;
    return single;
  }
  
  const input = [2, 2, 9, 7, 7, 6, 6, 5, 5, 5];
  const result = findUniq(input);
  console.log(result);

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