return the first non repeating character in a string in javascript

后端 未结 26 2459
清酒与你
清酒与你 2020-12-30 09:04

So I tried looking for this in the search but the closest I could come is a similar answer in several different languages, I would like to use Javascript to do it.

T

26条回答
  •  感动是毒
    2020-12-30 09:15

     function FirstNotRepeatedChar(str) {
     var arr = str.split('');
     var result = '';
     var ctr = 0; 
      for (var x = 0; x < arr.length; x++) {
       ctr = 0;
      for (var y = 0; y < arr.length; y++) {
      if (arr[x] === arr[y]) {
        ctr+= 1;
        }
       }
    
    if (ctr < 2) {
      result = arr[x];
      break;
      }
    }
    return result;
    }
    console.log(FirstNotRepeatedChar('asif shaik'));
    

提交回复
热议问题