Golang: find first character in a String that doesn't repeat

go

Solution

Using a map and 2 loops : `play`

func check(s string) string {
    m := make(map[rune]uint, len(s)) //preallocate the map size
    for _, r := range s {
        m[r]++
    }

    for _, r := range s {
        if m[r] == 1 {
            return string(r)
        }
    }
    return ""
}

The benfit of this is using just 2 loops vs multiple loops if you're using `strings.ContainsRune`, `strings.IndexRune` (each function will have inner loops in them).

Problem

I'm trying to write a function that returns the finds first character in a String that doesn't repeat, so far I have this: ``` package main import ( "fmt" "strings" ) func check(s string) string { ss := strings.Split(s, "") smap := map[string]int{} for i := 0; i < len(ss); i++ { (smap[ss[i]])++ } for k, v := range smap { if v == 1 { return k } } return "" } func main() { fmt.Println(check("nebuchadnezzer")) } ``` Unfortunately in Go when you iterate a map there's no guarantee of the order so every time I run the code I get a different value, any pointers?

Original source

Related problems