Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Find First Non-Repeating Character (Main Thread)

Here is the interview question prompt, presented for reference.

You're given a string of random alphanumerical characters with a length between 0 and 1000.

Write a method to return the first character in the string that does not repeat itself later on.

firstNonRepeat('asdfsdafdasfjdfsafnnunl') should return 'j'

Constraints

  • The given string can be empty
  • The string will only contain lowercase/uppercase alphabets and numerals
  • Expected time complexity : O(n)
  • Expected space complexity : O(A) where A is the number of ASCII characters

You can see the full challenge with visuals at this link.

Challenges • Asked over 3 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Nov 30, 2017:

This is the main discussion thread generated for Find First Non-Repeating Character.

Jean Commented on Jan 16, 2021:

Hello! Why does the 2nd loop of the solution went through char in charCounter instead of char in str?
Afaik javascript object does not always guarantee insertion order. Which means by looping through charCounter there is a possibility you might not hit the actual first non-repeating character but one of the other non-repeating character, if any. But I might be wrong, so let me know. Thanks!

See this for object property order in js.

Jake from AlgoDaily Commented on Jan 22, 2021:

Hey Jean,

My bad and sorry for the late response! You're 100% correct, I've updated the solution code and added a node in the tutorial to reflect this.