{"record":{"id":"76dfd1052a86472d","repo":"TheAlgorithms/Go","slug":"q-contains-same-character-q","errorCode":null,"errorMessage":"%q contains same character %q","messagePattern":"%q contains same character %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cipher/polybius/polybius.go","lineNumber":41,"sourceCode":"// If the size of \"chars\" is longer than \"size\",\n// \"chars\" are truncated to \"size\".\nfunc NewPolybius(key string, size int, chars string) (*Polybius, error) {\n\tif size < 0 {\n\t\treturn nil, fmt.Errorf(\"provided size %d cannot be negative\", size)\n\t}\n\tkey = strings.ToUpper(key)\n\tif size > len(chars) {\n\t\treturn nil, fmt.Errorf(\"provided size %d is too small to use to slice string %q of len %d\", size, chars, len(chars))\n\t}\n\tfor _, r := range chars {\n\t\tif (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {\n\t\t\treturn nil, fmt.Errorf(\"provided string %q should only contain latin characters\", chars)\n\t\t}\n\t}\n\tchars = strings.ToUpper(chars)[:size]\n\tfor i, r := range chars {\n\t\tif strings.ContainsRune(chars[i+1:], r) {\n\t\t\treturn nil, fmt.Errorf(\"%q contains same character %q\", chars[i+1:], r)\n\t\t}\n\t}\n\n\tif len(key) != size*size {\n\t\treturn nil, fmt.Errorf(\"len(key): %d must be as long as size squared: %d\", len(key), size*size)\n\t}\n\treturn &Polybius{size, chars, key}, nil\n}\n\n// Encrypt encrypts with polybius encryption\nfunc (p *Polybius) Encrypt(text string) (string, error) {\n\tencryptedText := \"\"\n\tfor _, char := range strings.ToUpper(text) {\n\t\tencryptedChar, err := p.encipher(char)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed encipher: %w\", err)\n\t\t}\n\t\tencryptedText += encryptedChar","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/cipher/polybius/polybius.go#L23-L59","documentation":"NewPolybius requires unique characters in the (truncated, uppercased) alphabet; a repeated rune would map several key positions to the same coordinate, so the constructor reports the suffix containing the duplicate.","triggerScenarios":"Thrown at cipher/polybius/polybius.go:41 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Deduplicate chars before passing them (e.g. preserve first occurrence order)","Use a standard 25-letter alphabet with I/J merged","Reject user-supplied alphabets containing duplicates"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}