Dr Gomathi
Write a Python function that finds the first non-repeating character in a given string. If every character repeats, return None. The function should be case-sensitive, meaning 'A' and 'a' should be considered different characters.
By Dr Gomathi in Python on Jan 19 2024
  • Lokendra Singh
    May, 2024 11

    1. def first_non_repeating_char(string):
    2. char_count = {}
    3. # Count the occurrences of each character in the string
    4. for char in string:
    5. char_count[char] = char_count.get(char, 0) + 1
    6. # Find the first character with count 1
    7. for char in string:
    8. if char_count[char] == 1:
    9. return char
    10. return None
    11. input_string = "aaAbbccddeeffK"
    12. result = first_non_repeating_char(input_string)
    13. print("First non-repeating character:", result) # Output: A

    • 0
  • Ganesh Kavhar
    Jan, 2024 23

    please find below solution
    `string = “hello”
    index = -1
    fnc = “”

    if len(string) == 0 :
    print(“EMTPY STRING”);

    for i in string:
    if string.count(i) == 1:
    fnc += i
    break
    else:
    index += 1
    if index == len(string)-1 :
    print(“All characters are repeating “)
    else:
    print(“First non-repeating character is”, fnc)`

    • 0
  • Manikandan Murugesan
    Jan, 2024 22

    def first_non_repeating_character(input_string):char_count = {}# Count the occurrences of each character in the stringfor char in input_string:char_count[char] = char_count.get(char, 0) 1# Find the first non-repeating characterfor char in input_string:if char_count[char] == 1:return char# If every character repeats, return Nonereturn None# Example usage: input_str = "abacabad" result = first_non_repeating_character(input_str)if result is not None:print(f"The first non-repeating character is: {result}") else:print("Every character repeats.")

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS