Last active
July 7, 2020 13:14
-
-
Save r3code/0882faaff1084a73042a850183becb4f to your computer and use it in GitHub Desktop.
Go: check if string has duplicate symbols
This file contains hidden or 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
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