first repeated character in a string javascript 1

first repeated character in a string javascript

function firstRepeatingCharacter(str) {
  for (let i = 0; i < str.length; i++) {
    if (str.indexOf(str.charAt(i)) !== str.lastIndexOf(str.charAt(i))) {
      return str.charAt(i)
    }
  }
  return 'no results found'
}

Here is what the above code is Doing:
1. We loop through the string
2. We check if the index of the current character is not equal to the last index of the current character
3. If the above condition is true, we return the current character
4. If the above condition is false, we return 'no results found'

Similar Posts