Answers for "Given a string, find its first non-repeating character"

0

First non repeating character position in a string

# First non repeating character position in a string

def first_non_repeating_letter(str)
    strL = str.downcase
    strL.each_char.with_index do |v, i|
        if strL.index(v) == strL.rindex(v)
            return i
        end
    end
    -1
end

print "First non repeating character position = ", 
	first_non_repeating_letter("abcdcba");  # 3
Posted by: Guest on October-07-2021
0

First non repeating character position in a string

// First non repeating character position in a string

function first_non_repeating_letter(str) {
  for(var i = 0; i < str.length; i++) {
    if (str.indexOf(str[i]) === str.lastIndexOf(str[i])) {
      return i
    }
  }
  return null
}

console.log("First non repeating character position = " + 
    first_non_repeating_letter("abcdcba"));  //3
Posted by: Guest on October-08-2021

Code answers related to "Given a string, find its first non-repeating character"

Browse Popular Code Answers by Language