Skip to content

Instantly share code, notes, and snippets.

@r3code
Last active July 7, 2020 13:14
Show Gist options
  • Save r3code/0882faaff1084a73042a850183becb4f to your computer and use it in GitHub Desktop.
Save r3code/0882faaff1084a73042a850183becb4f to your computer and use it in GitHub Desktop.
Go: check if string has duplicate symbols
func hasDuplicateSymbols(s string) bool {
seen := make(map[rune]struct{}, len(s))
for _, r := range s {
if _, ok := seen[r]; ok {
return true
}
seen[r] = struct{}{}
}
return false
}
// hasDuplicateSymbols("") -> true
// hasDuplicateSymbols("abc") -> true
// hasDuplicateSymbols("abca") -> false
// hasDuplicateSymbols(" a ") -> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment