Home > front end >  Search for special Numbers in an array using Javascript
Search for special Numbers in an array using Javascript

Time:09-26

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);

CodePudding user response:

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);

CodePudding user response:

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)

CodePudding user response:

const printSpecialNumbers = (numbers = []) => numbers.reduce((acc,
  number, idx, all) => {
  if (all.filter(s => s === number).length < 2) {
    acc = [...acc, number]
  }
  return acc
}, [])

CodePudding user response:

You need to sort the array first in java script this is how it will work

    let arr=[1,5,1,7]
    let specialNumber:any[]=[]
    arr=arr.sort((a,b)=>(a-b))
    let prevoiusNumber:any=null
    arr.map(e=>{
        if( prevoiusNumber && prevoiusNumber!==e){
            specialNumber.push(e)
            
        }
        prevoiusNumber=e
    })
    console.log(specialNumber)

Copy this code and run this

  • Related