Determine if a string has all unique characters: Difference between revisions

Content added Content deleted
(β†’β€Ž{{header|Perl}}: works better with Unicode combining characters)
(julia example)
Line 160: Line 160:
'🐑' (0x1f421) is duplicated at positions 3 and 8.
'🐑' (0x1f421) is duplicated at positions 3 and 8.
</pre>
</pre>

=={{header|Julia}}==
<lang julia>arr(s) = [c for c in s]
alldup(a) = filter(x -> length(x) > 1, [findall(x -> x == a[i], a) for i in 1:length(a)])
firstduplicate(s) = (a = arr(s); d = alldup(a); isempty(d) ? nothing : first(d))

function testfunction(strings)
println("String | Length | All Unique | First Duplicate | Positions\n" *
"-------------------------------------------------------------------------------------")
for s in strings
n = firstduplicate(s)
a = arr(s)
println(rpad(s, 38), rpad(length(s), 11), n == nothing ? "yes" :
rpad("no $(a[n[1]])", 26) * rpad(n[1], 4) * "$(n[2])")
end
end

testfunction([
"",
".",
"abcABC",
"XYZ ZYX",
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ",
"hΓ©tΓ©rogΓ©nΓ©itΓ©",
"πŸŽ†πŸŽƒπŸŽ‡πŸŽˆ",
"πŸ˜πŸ˜€πŸ™ŒπŸ’ƒπŸ˜πŸ™Œ",
"πŸ πŸŸπŸ‘πŸ¦ˆπŸ¬πŸ³πŸ‹πŸ‘",
])
</lang>{{out}}
<pre>
String | Length | All Unique | First Duplicate | Positions
-------------------------------------------------------------------------------------
0 yes
. 1 yes
abcABC 6 yes
XYZ ZYX 7 no X 1 7
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ 36 no 0 10 25
hΓ©tΓ©rogΓ©nΓ©itΓ© 13 no Γ© 2 4
πŸŽ†πŸŽƒπŸŽ‡πŸŽˆ 4 yes
πŸ˜πŸ˜€πŸ™ŒπŸ’ƒπŸ˜πŸ™Œ 6 no 😍 1 5
πŸ πŸŸπŸ‘πŸ¦ˆπŸ¬πŸ³πŸ‹πŸ‘ 8 no 🐑 3 8
</pre>



=={{header|Perl}}==
=={{header|Perl}}==