:::: MENU ::::

JavaScript job interview question – Find first non repeated character in string

js-code-interview

There are several way to code this puzzle. Some of them are time intensive and some of them are space intensive.

I’ll solve it here using javascript array (hash table in computer science term).

Sample Input & output:

Programming Logic

  • Scan whole string and store each character in “hash table” as key with their initial value = 0
  • If character is already stored into “hash table”, increment its value. (i.e 1)
  • If at-least one character repetition found, set flag (i.e atleast_one_found = true)
  • Now scan hash table in-case above atleast_one_found = true.
  • whenever first hash key value=0 found, return that key as first non-repeated character.

Code

HTML Form to get input string.

JavaScript to find first non repeated character in string

Final Output


So, what do you think ?

  •