return the first non repeating character in a string in javascript

后端 未结 26 2381
清酒与你
清酒与你 2020-12-30 09:04

So I tried looking for this in the search but the closest I could come is a similar answer in several different languages, I would like to use Javascript to do it.

T

26条回答
  •  自闭症患者
    2020-12-30 09:22

    First of all, start your loop at 1, not 0. There is no point in checking the first character to see if its repeating, obviously it can't be.

    Now, within your loop, you have someString[i] and someString[i - 1]. They are the current and previous characters.

    if someString[i] === someString[i - 1] then the characters are repeating, if someString[i] !== someString[i - 1] then they are not repeating, so you return someString[i]

    I won't write the whole thing out for you, but hopefully the thought process behind this will help

提交回复
热议问题