πΊπΈ Given an arbitrary input string, return the first nonrepeated character in the string.
π¦π· Dado una palabra u oracion aleatoria, retorne la primer letra no repetida en dicha palabra u oracion.
π°π· μμ λ¬Έμμ΄μ μΈμλ‘ λ°μ κ²½μ° λ¬Έμμ΄μ 첫 λ²μ§Έ λΉμ€λ³΅ λ¬Έμλ₯Ό λ°ννμμμ€.
Example:
stNonRepChar('ABA'); // => 'B' stNonRepChar('AACBDB'); // => 'C'
# START [ here ] π
var stNonRepChar = function(str) {
// Your CODE
}
// Test
stNonRepChar("AACBDB");
solution π
.
.
.
.
.
.
.
.
.
.
.
var stNonRepChar = function(str) {
var counter = {};
for (let i = 0; i < str.length; i++) {
if (counter[str[i]]) {
counter[str[i]]++;
} else {
counter[str[i]] = 1;
}
}
for (let i = 0; i < str.length; i++) {
if (counter[str[i]] === 1) {
return str[i];
}
}
}
// Test
console.log(stNonRepChar("AACBDB"));
var stNonRepChar = function(str) {
for (let char of str) {
if (str.match(new RegExp(char, "gi")).length == 1) {
return char;
}
}
}
// Test
console.log(stNonRepChar("AACBDB"));
var stNonRepChar = function(str) {
var strTrim = str.split("");
var result = "";
var counter = 0;
for (var x = 0; x < strTrim.length; x++) {
counter = 0;
for (var y = 0; y < strTrim.length; y++) {
if (strTrim[x] === strTrim[y]) {
counter += 1;
}
}
if (counter < 2) {
result = strTrim[x];
break;
}
}
return result;
}
// Test
console.log(stNonRepChar("AACBDB"));