How to find the first non-repeated character in a string with Node.js

2 Answers

0 votes
function getFirstNonRepeatedCharacter(s) {
    for (let ch of s) {
        if ([...s].filter(c => c === ch).length === 1) {
            return ch;
        }
    }
    return null;
}

console.log(getFirstNonRepeatedCharacter("ppdadxefe")); 
      



/*
run:

a

*/

 



answered Jun 28 by avibootz
0 votes
function getFirstNonRepeatedCharacter(s) {
    let length = s.length;
     
    for (let i = 0; i < length; i++) {
        if(s.indexOf(s[i]) === s.lastIndexOf(s[i])) {
            return s[i];
        }
    }
     
    return null;
}
  
const s = 'ppdadxefe';
  
console.log(getFirstNonRepeatedCharacter(s));




/*
run:

a

 



answered Jun 28 by avibootz
...