Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!