Anonymous user
Determine if a string has all unique characters: Difference between revisions
Determine if a string has all unique characters (view source)
Revision as of 21:28, 19 November 2020
, 3 years ago→{{header|Nim}}
(→{{header|Haskell}}: Tidied, replaced an Arrow with an Applicative) |
|||
Line 2,157:
Not all characters in the string are unique.
'0' (0x30) is duplicated at positions 25 and 10</pre>
=={{header|Nim}}==
<lang Nim>import unicode, strformat
proc checkUniqueChars(s: string) =
echo fmt"Checking string ""{s}"":"
let runes = s.toRunes
for i in 0..<runes.high:
let rune = runes[i]
for j in (i+1)..runes.high:
if runes[j] == rune:
echo "The string contains duplicate characters."
echo fmt"Character {rune} ({int(rune):x}) is present at positions {i+1} and {j+1}."
echo ""
return
echo "All characters in the string are unique."
echo ""
const Strings = ["",
".",
"abcABC",
"XYZ ZYX",
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ",
"hétérogénéité",
"🎆🎃🎇🎈",
"😍😀🙌💃😍🙌",
"🐠🐟🐡🦈🐬🐳🐋🐡"]
for s in Strings:
s.checkUniqueChars()</lang>
{{out}}
<pre>Checking string "":
All characters in the string are unique.
Checking string ".":
All characters in the string are unique.
Checking string "abcABC":
All characters in the string are unique.
Checking string "XYZ ZYX":
The string contains duplicate characters.
Character X (58) is present at positions 1 and 7.
Checking string "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ":
The string contains duplicate characters.
Character 0 (30) is present at positions 10 and 25.
Checking string "hétérogénéité":
The string contains duplicate characters.
Character é (e9) is present at positions 2 and 4.
Checking string "🎆🎃🎇🎈":
All characters in the string are unique.
Checking string "😍😀🙌💃😍🙌":
The string contains duplicate characters.
Character 😍 (1f60d) is present at positions 1 and 5.
Checking string "🐠🐟🐡🦈🐬🐳🐋🐡":
The string contains duplicate characters.
Character 🐡 (1f421) is present at positions 3 and 8.</pre>
=={{header|Perl}}==
|