0

Trying to find the key in the object that maps to the value "1".

As it stands the function returns undefined.

This is the expected behavior.

input : deliveryIds: [5,4,5,3,3]
output: 4

This is the code that iterates through the Object searching for a key that maps to "1".

Any help on what i need to change is much appreciated.

function findMissingQuad(deliveryIds: number[]) {
  const idsToOccurrences = {};

  deliveryIds.forEach(id => {
    if (idsToOccurrences[id]) {
      idsToOccurrences[id]++;
    } else {
      idsToOccurrences[id] = 1 || [];
    }
  });

  return Object.keys(idsToOccurrences).forEach(id => {
    if (idsToOccurrences[id] === 1) {
      return id;
    }
  });
}

mangokitty
  • 1,759
  • 3
  • 12
  • 17
  • What if there's more than one? `[5, 4, 5, 2, 3, 5]` will have both `4` and `2` as possible results. – T.J. Crowder Nov 01 '19 at 16:28
  • `.forEach` always returns `undefined`, regardless of any `return` statements in it. [More information](https://stackoverflow.com/questions/34653612/what-does-return-keyword-mean-inside-foreach-function) – VLAZ Nov 01 '19 at 16:29
  • @T.J.Crowder that behavior is ok as long as it identifies the non duplicates. – mangokitty Nov 01 '19 at 16:30
  • @VLAZ Thank you. TypeScript doesn't seem to have support for find() which I wanted to use. Any advice on what might work here? I need to return the value, not true or false. – mangokitty Nov 01 '19 at 16:33
  • Wait, how does *TypeScript* not support `.find`? TypeScript has almost no functionality by itself - it has the type system and some extra syntax but all that goes away after compilation and is replaced with plain JavaScript. Most notably, TS doesn't add or remove anything to the object prototypes, so [`Array#find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) will not be affected by TS. – VLAZ Nov 01 '19 at 16:39
  • @VLAZ I see, I apologise. I think I might have to add "es6" into the lib section of tsconfig.json or something as I have the error TS2339: "Property 'find' does not exist on type"'string[]' – mangokitty Nov 01 '19 at 16:41
  • @mangokitty - `Array.prototype.find` was indeed added in ES2015 (aka "ES6"). – T.J. Crowder Nov 01 '19 at 16:43

1 Answers1

0

One solution would be to keep a set of numbers you've seen at all, and another set of numbers you've seen just once, and remove a number from the second set if you've seen it before:

function findMissingQuad(deliveryIds/*: number[]*/) {
    const seen = new Set();
    const once = new Set();
    for (const id of deliveryIds) {
        if (seen.has(id)) {
            // We'e seen this one before
            once.delete(id);
        } else {
            // First time we've seen this one
            seen.add(id);
            once.add(id);
        }
    }
    return [...once];
}

console.log(findMissingQuad([5, 4, 5, 3, 3]));
console.log(findMissingQuad([5, 4, 5, 2, 3, 3]));

Note that I've had it return an array, since as you can see from my second example, it's possible to have more than one unique value in the array.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875