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

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

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 Report

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 Report

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 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!