题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
function getOnceChar(str) { const strArr = str.split(''); let left = ''; let right = ''; for (let i = 0, len = strArr.length; i < len; i++) { if (i === 0) { left = ''; right = str.substring(1); } else if (i === strArr.length - 1) { right = ''; left = str.substring(0, strArr.length - 1); } else { left = str.substring(0, i); right = str.substring(i + 1); } if (left.indexOf(strArr[i]) === -1 && right.indexOf(strArr[i]) === -1) { return strArr[i]; } } return -1; } while(line = readline()) { print(getOnceChar(line)); }