Created
July 1, 2014 19:20
-
-
Save jonbodner/adb93a5f8652fea46eca to your computer and use it in GitHub Desktop.
First non-repeated character in a string in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func firstNonRepeatChar(s string) rune { | |
useMap := make(map[rune]bool) | |
for _, v := range s { | |
_, ok := useMap[v] | |
useMap[v] = ok | |
} | |
for _, v := range s { | |
if !useMap[v] { | |
return v | |
} | |
} | |
return rune(0) | |
} | |
func main() { | |
fmt.Printf("%c",firstNonRepeatChar("abca")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment