1

I'm getting an error and have idea why 'count' is returning an error. Please help.

Write a function called findTwins, which accepts an array of integers and finds two of same numbers and returns the number that is repeated twice. The function should return null if there is not a number repeated twice.

function findTwins(arr) {
  if (arr.length === 0) return null;
  let count = {};
  //count occurances of each item
  for (let i = 0; i < arr.length; i++) {
    let num = arr[i];
    //if element isn't in given key, assign it value '0'
    if (count[num] === undefined) {
      count[num] = 0;
    } //if element has occured in object, increase it by 1
    count[num]++;
  }

  //iterate through count object   
  for (let key in count) {
    //if value of count is 2, returns that key
    if (count[key] === 2) {
      return key;

      //if there are no repeats in array, return null    
    } else {
      (count[key] === 0) {
        return null;
      }
    }
  }
}

console.log(
  findTwins([2, 3, 6, 34, 7, 8, 2]), // 2
  findTwins([]), // null
  findTwins([3, 1, 4, 2, 5]) // null
)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
elyse
  • 11
  • 1

3 Answers3

2
  • You are missing an 'if' in (count[key] === 0) {

  • You need to return null at the end of the function to handle a non-match

  • I would not use for (let key in count) { of an array - there are much safer and better ways to iterate an array than the object traverse

  • Reduce or filter are better methods to process your array:

Here is a solution based on https://stackoverflow.com/a/35922651/295783

function findTwins(arr) {
  let ret = null;
  if (arr && arr.length) ret = arr.filter(function(value, index, self) {
    return (self.indexOf(value) !== index)
  })
  return !ret || ret.length == 0 ? null : ret;
}

console.log(
  findTwins([2, 3, 6, 34, 7, 8, 2]), // 2
  findTwins([]), // null
  findTwins([3, 1, 4, 2, 5]) // null
);

Her is a version based on the suggestion in https://stackoverflow.com/a/54966920/295783

function findTwins(arr) {
  let res = [];
  if (arr && arr.length) {
    for (let i = 0; i < arr.length; i++) {
      var num = arr[i];
      if (arr.indexOf(num) !== arr.lastIndexOf(num) &&
          res.indexOf(num) === -1) res.push(num)
    }
  }
  return res.length ? res : null;
}


console.log(
  findTwins([2, 3, 6, 34, 7, 8, 2]), // 2
  findTwins([]), // null
  findTwins([3, 1, 4, 2, 5]), // null
  findTwins([3, 1, 2,5,4, 2, 5]) // null
)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

You have a syntax error here you missed if

else {
      (count[key] === 0) {  //<- here you missed if
        return null;
      }
    }

Better way is to use reduce

function findTwins(input){
  let twins = input.reduce((op,inp)=>{
    if(op.entry[inp]){
      op.entry[inp]++;
      if(op.entry[inp] === 2){
        op.twins.push(inp)
      }
    } else{
      op.entry[inp] = 1
    }
    return op
  },{twins:[],entry:{}})
  return twins.twins.length > 0 ? twins.twins : null
}

console.log(
  findTwins([2, 3, 6, 34, 7, 8, 2]), // 2
  findTwins([]), // null
  findTwins([3, 1, 4, 2, 5]), // null
  findTwins([3, 1, 4, 2, 5,5,2,3,1,4])
)

If you want to return only numbers which appear exactly twice than you can loop at this example

function findTwins(input){
  let twins = input.reduce( (op,inp) => ( (op[inp]? op[inp]++ : op[inp] = 1),op),{})
  let final =  Object.keys(twins).reduce((op,key) => ((twins[key]===2 ? op.push(+key) : op ),op),[])
  return final.length > 0 ? final : null
}

console.log(
  findTwins([2, 3, 6, 34, 7, 8, 2]), // 2
  findTwins([]), // null
  findTwins([3, 1, 4, 2, 5, 5, 5, 5]), // null
)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • @mplungjan but this is different in many aspect from your answer mate. i am using reduce in stead of filter and index of. your answer needs double loop over the array one by filter and second by indexof. while this one is not. and moreover i can shorten the statement in reduce but readability matters. i have posted it considering an alternate solution – Code Maniac Mar 03 '19 at 08:28
  • @mplungjan what do you mean by that mate ? could please explain it bit. all it is doing is just comparison which have almost negligible impact – Code Maniac Mar 03 '19 at 08:46
  • Ok, I had to wrap my head around this: `{twins:[],entry:{}}` – mplungjan Mar 03 '19 at 09:13
1

Using indexOf and lastIndexOf we can get the position and through that get the repeated elements in array.

var x = [2, 3, 6, 34, 7, 8, 2]

const c = x.filter(v => {
  if (x.indexOf(v) !== x.lastIndexOf(v)) {
    x.splice(x.lastIndexOf(v))
    return v;
  }
})
console.log(c)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
viren
  • 44
  • 5