TheAlgorithms/JavaScript · error · TypeError
Duplicate characters in character set are not allowed
Error message
Duplicate characters in character set are not allowed
What it means
Thrown by convertArbitraryBase after the type guard passes, when either baseOneCharacterString or baseTwoCharacterString contains repeated characters. Uniqueness is required because each character maps bijectively to a digit value; a duplicate would make the value of a digit ambiguous and break both encoding and decoding. The check compares array length against a Set of the same characters.
Source
Thrown at Conversions/ArbitraryBase.js:39
const convertArbitraryBase = (
stringInBaseOne,
baseOneCharacterString,
baseTwoCharacterString
) => {
if (
[stringInBaseOne, baseOneCharacterString, baseTwoCharacterString]
.map((arg) => typeof arg)
.some((type) => type !== 'string')
) {
throw new TypeError('Only string arguments are allowed')
}
const baseOneCharacters = [...baseOneCharacterString]
const baseTwoCharacters = [...baseTwoCharacterString]
for (const charactersInBase of [baseOneCharacters, baseTwoCharacters]) {
if (charactersInBase.length !== new Set(charactersInBase).size) {
throw new TypeError(
'Duplicate characters in character set are not allowed'
)
}
}
const reversedStringOneChars = [...stringInBaseOne].reverse()
const stringOneBase = baseOneCharacters.length
let value = 0
let placeValue = 1
for (const digit of reversedStringOneChars) {
const digitNumber = baseOneCharacters.indexOf(digit)
if (digitNumber === -1) {
throw new TypeError(`Not a valid character: ${digit}`)
}
value += digitNumber * placeValue
placeValue *= stringOneBase
}
const outputChars = []
const stringTwoBase = baseTwoCharacters.lengthView on GitHub (pinned to 5c39e87a9a)
Solutions
- Audit each character-set string and remove duplicates (e.g. [...set].join('')).
- Generate alphabets programmatically from a known-unique source, e.g. '0123456789abcdef...', rather than typing them.
- Add a pre-flight assertion in your caller: new Set(alpha).size === alpha.length.
Example fix
// before
convertArbitraryBase('10', '01', '0011')
// after
const dedupe = (s) => [...new Set([...s])].join('')
convertArbitraryBase('10', '01', dedupe('0011')) // -> '01' Defensive patterns
Strategy: validation
Validate before calling
const hasUniqueChars = (s) => [...s].length === new Set([...s]).size
if (!hasUniqueChars(srcAlpha) || !hasUniqueChars(dstAlpha)) {
throw new Error('Character set must have unique glyphs')
}
convertArbitraryBase(input, srcAlpha, dstAlpha) Type guard
const isUniqueCharset = (s) => typeof s === 'string' && [...s].length === new Set([...s]).size
Try / catch
try {
convertArbitraryBase(input, srcAlpha, dstAlpha)
} catch (e) {
if (/Duplicate characters/.test(e.message)) {
srcAlpha = [...new Set([...srcAlpha])].join('')
dstAlpha = [...new Set([...dstAlpha])].join('')
return convertArbitraryBase(input, srcAlpha, dstAlpha)
}
throw e
} Prevention
- Generate alphabets from a known-unique source rather than hand-typing.
- Add a Set-size assertion in tests for every charset constant.
- Deduplicate at the boundary so the converter never sees duplicates.
When it happens
Trigger: Calling convertArbitraryBase('10', '01', '0011') (output set '0011' has duplicate 0 and 1), or a source alphabet like '01234567890' with a repeated 0. Either character set triggering the duplicate is sufficient since the loop covers both arrays.
Common situations: Hand-typing a custom alphabet and accidentally repeating a glyph; reusing a charset constant that was concatenated incorrectly; using multibyte/emoji strings where a grapheme got duplicated; building a base alphabet from a range with an off-by-one overlap.
Related errors
- Not a valid character: ${digit}
- Only string arguments are allowed
- Argument is not a valid HEX code!
- Invalid hex string.
- Invalid units
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/63fc198315a34fa9.
Report an issue: GitHub.