First non repeat characters

Our Own Score

In this JavaScript program, we will write a function that accepts a string and then return the first non-repeating character in the string. For example, if we enter “helloh” in the function, we will get the character ‘e’ Because ‘e’ appears before ‘o’. If all the characters in the string are repeating, the function will return an empty string. Uppercase and lowercase characters are considered to be the same, so ‘t’ is same as ‘T’!

The following is the entire function…

function firstNonRepeatingLetter(s) {
	
	let repeat = s.split("")
	let first;
	let temp = [] // we use this array to keep the character which has already been gone through before 
	let isnotrepeat = true;
	
	for(let i = 0; i < repeat.length; i++) {
		first = repeat[i];
		if(temp.includes(first))
			continue
		for(let j = i + 1; j < repeat.length; j++) {
			if(repeat[j].toLowerCase() == first.toLowerCase()) { // uppercase and lowercase character is considered to be the same 
				temp.push(first)
				isnotrepeat = false;
				break;
			}
		}
		
		if(isnotrepeat)
			return first;
		isnotrepeat = true;
	}
	
	return ""
}

If we enter the string “Good Morning” in the above function, we will get ‘d’.

0

Leave a Reply

avatar
  Subscribe  
Notify of