Blog

Find Unique Characters in a String
Posted on June 19, 2019 in Algorithms, JavaScript by Matt Jennings

Find the First Non-repeating Character in a String

var string = 'what a wonderful day it has been!';

function firstNonRepeatedChar(str) {
  for(var i = 0; str.length; i++) {
    var char = string[i];
    var count = 0;
    
    for(var j = 0; j < str.length; j++) {
      if(str[j] === char) {
        count++;
      }
    }
    
    if(count === 1) {
      return char;
    }
    
  }

  
}

console.log(firstNonRepeatedChar(string));
// "o"

 

Leave a Reply