first non repeating character problem in javascript code example
Example 1: first non repeating character javascript
function firstNotRepeatingCharacter(s) {
for (let i = 0; i < s.length; i++) {
if(s.indexOf(s.charAt(i)) == s.lastIndexOf(s.charAt(i))) {
return s.charAt(i)
}
}
return '_'
}
Example 2: 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'
}