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

Content added Content deleted
(Add Erlang version)
(Added solution for Action!)
Line 73: Line 73:
XYZ ZYX 7 no 'Z' 5A 3 5
XYZ ZYX 7 no 'Z' 5A 3 5
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ 36 no '0' 30 10 25
1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ 36 no '0' 30 10 25
</pre>

=={{header|Action!}}==
<lang Action!>PROC PrintBH(BYTE a)
BYTE ARRAY hex=['0 '1 '2 '3 '4 '5 '6 '7 '8 '9 'A 'B 'C 'D 'E 'F]

Put(hex(a RSH 4))
Put(hex(a&$0F))
RETURN

PROC Test(CHAR ARRAY s)
BYTE i,j,n,pos1,pos2

pos1=0 pos2=0
n=s(0)-1
IF n=255 THEN n=0 FI
FOR i=1 TO n
DO
FOR j=i+1 TO s(0)
DO
IF s(j)=s(i) THEN
pos1=i
pos2=j
EXIT
FI
OD
IF pos1#0 THEN
EXIT
FI
OD

PrintF("""%S"" (len=%B) -> ",s,s(0))
IF pos1=0 THEN
PrintE("all characters are unique.")
ELSE
PrintF("""%C"" (hex=$",s(pos1))
PrintBH(s(pos1))
PrintF(") is duplicated at pos. %B and %B.%E",pos1,pos2)
FI
PutE()
RETURN

PROC Main()
Test("")
Test(".")
Test("abcABC")
Test("XYZ ZYX")
Test("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ")
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Determine_if_a_string_has_all_unique_characters.png Screenshot from Atari 8-bit computer]
<pre>
"" (len=0) -> all characters are unique.

"." (len=1) -> all characters are unique.

"abcABC" (len=6) -> all characters are unique.

"XYZ ZYX" (len=7) -> "X" (hex=$58) is duplicated at pos. 1 and 7.

"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (len=36) -> "0" (hex=$30) is duplicated at pos. 10 and 25.
</pre>
</pre>