How to find the first character that is repeated in a string with JavaScript

1 Answer

0 votes
function get_first_repeated_character(s) {
    const len = s.length;

    for (let i = 0; i < len; i++) {
         for (let j = 0; j < i; j++) {
            if (s[i] === s[j]) {
                return s[i];
            }
        }
    }

    return "-1";
}

const s = "abcdxypcbop"; 

console.log(get_first_repeated_character(s));


   
/*
run:
   
c
   
*/

 



answered Jan 26, 2020 by avibootz
edited Dec 2, 2024 by avibootz

Related questions

...