How to find the first non repeating character in a string ? Technical Interview Question

This is the most common question asked in technical interview. So the problem statement is - 
Let the string is "letter" then the first non repeating character will be "l". If the string is "stress" then the first non repeating character will be "t".

Now you must be clear with the problem statement. So the algorithm will be  - 
 

1.   Create the  character count hash table .
         For each character
            If no value stored in the character 
                     set it to 1 .
            else 
                     increment the value of the character by 1 .

2.  Scan the string
           For each character
           return character if the count in hash table is 1 .
           If no character have count 1 , return null

And the implementation is(Example is given in GOLANG(Go Programming Language)) - 

package main

import (
    "fmt"
    "os"
)

func main() {
    str := "stress"
    stringMap := make(map[string]int)
    lenght := len(str)
    for i := 0; i < lenght; i++ {
        t := fmt.Sprintf("%q", str[i])
        if _, ok := stringMap[t]; ok {
            stringMap[t] = stringMap[t] + 1
        } else {
            stringMap[t] = 1
        }

    }

    for i := 0; i < lenght; i++ {
        t := fmt.Sprintf("%q", str[i])
        if stringMap[t] == 1 {
            fmt.Println("First non repeating character is - " + t)
            os.Exit(0)
      
  }
 
   }
    fmt.Println("NULL")
}

And the Output is  - First non repeating character is - 't'

Hope this will help you. For more follow us on facebook, linkedin, twitter.