0
function removeDups(arr) {
  let x = {};
    for (let i=0; i<arr.length; i++)  {
      if(!x[arr[i]]) {
        x[arr[i]] = true;
      }
    }
  let result = Object.keys(x);

  for(let i=0; i<result.length; i++) {
    if(typeof result[i] == Number) {
     result[i] = parseInt(result[i]);
    }
  }
  return result;
}


removeDups([1, 0, 1, 0]);
  //➞ [1, 0]

Hey guys trying to return [1,0] like the problem states but I keep returning ['1','0']. I'm trying to convert these values to numbers and not having much luck. Any help?

Brixsta
  • 605
  • 12
  • 24
  • 1
    `if(typeof result[i] == Number)` you maybe meant `if (typeof result[i] !== 'number')`? PS: unless you use symbols, property keys are always strings, so the check isn't even necessary - it's certainly a string, unless the array contains symbols, and then you'd have to find a different way than `parseInt` anyways. – ASDFGerte May 02 '21 at 23:44
  • 1
    You could use a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) – Thomas Sablik May 02 '21 at 23:44
  • 1
    Note that `typeof` always results in a string (`"number"`) not a constructor function (`Number`). – Heretic Monkey May 02 '21 at 23:48
  • 1
    See also [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/q/1960473/215552) for more answers on how to remove duplicates. – Heretic Monkey May 02 '21 at 23:51
  • 1
    It's kind of sad to see, that the top rated answer in the main dupe target is primarily advocating O(n²) solutions (mentions another, but doesn't explain the difference at all), while answers like https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array/9229821#9229821 exist. – ASDFGerte May 03 '21 at 00:03
  • @ASDFGerte I think you're confusing "accepted" and "top rated". The "accepted" answer, with a score of 491, is old. The "top rated" answer, that you link to, with a score of 4375, has almost an order of magnitude higher score. I don't find that sad at all. What's disconcerting is that the accepted answer is pinned to the top and has a green checkmark. – Heretic Monkey May 03 '21 at 01:25
  • I am referring to the linked dupe target, https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates, in favor of which https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array got closed as dupe. – ASDFGerte May 03 '21 at 01:27

0 Answers0