5

I am trying to map the repetition of letters into a hashmap and then returning the first non-repeated character in a string. Following is the function I've written to do so:

export const firstNonRepeatedFinder = (aString: string): string | null => {
  const lettersMap: Map<string, number> = new Map<string, number>();

  for (let letter of aString) {
    if (!lettersMap.has(letter)) {
      lettersMap.set(letter, 1);
    } else {
      incrementLetterCount(letter, lettersMap);
    }
  }

  for (let letter of aString) {
    if (lettersMap.get(letter) === 1) return letter;
  }

  return null;

  function incrementLetterCount(
    aLetter: string,
    aLettersHashMap: Map<string, number>
  ): void {
    if (
      aLettersHashMap.has(aLetter) &&
      aLettersHashMap.get(aLetter) !== undefined
    ) {
      aLettersHashMap.set(aLetter, aLettersHashMap.get(aLetter) + 1);
    }
  }
};

However, in incrementLetterCount, function, although I am handling to exclude the undefined values for getting a key in the hashmap, it still complaining about Object is possibly 'undefined' which means that the get method is might return undefined and I cannot proceed with it.

Does anyone know what I am missing here that results in this error?

4
  • That's weird, but does if (aLettersHashMap.has(aLetter) && aLettersHashMap.get(aLetter)) work? Just test its truthiness.
    – cs95
    Jun 7, 2020 at 22:00
  • Then again it also seems like you're missing the else { aLettersHashMap.set(aLetter, 1); } case that should do the initialization.
    – cs95
    Jun 7, 2020 at 22:01
  • @cs95 yes, if (aLettersHashMap.has(aLetter) && aLettersHashMap.get(aLetter)) is true. I am not sure if I fully understood the second comment. But do you mean that in the else statement I should have instantiated { aLettersHashMap.set(aLetter, 1); }? in more details: else { lettersMap.set(letter, 1); incrementLetterCount(letter, lettersMap); } if so, this will always default the aLetter counter to 1
    – Amirsalar
    Jun 9, 2020 at 5:18
  • @cs95 just another update: even aLettersHashMap.get(aLetter) has a value of 1 when I console.log() it in the incrementation loop, before incrementing it, meaning that they are instantiated
    – Amirsalar
    Jun 9, 2020 at 6:21

2 Answers 2

6

I found the following solution to my problem but still not sure why the original code snippet was throwing a compile-time error (although the code was checking against undefined values).

It seems that in Typescript we can assert that the operand is not null/undefined (from here).

A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded.

  function incrementLetterCount(
    aLetter: string,
    aLettersHashMap: Map<string, number>
  ): void {
      aLettersHashMap.set(aLetter, aLettersHashMap.get(aLetter)! + 1);
  }
-3

Another reason is the tsconfig.json might have the following settings

"strict": true, /* Enable all strict type-checking options. */

if you take out the line, the error should go away.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.