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

Add Erlang version
(Added 11l)
(Add Erlang version)
Line 1,155:
String contains a repeated character.
Character "0" (hex 0030) occurs at positions 11 and 26.</pre>
 
=={{header|Erlang|Erlang}}==
<lang erlang>
-module(string_examples).
-export([all_unique/1, all_unique_examples/0]).
 
all_unique(String) ->
CharPosPairs = lists:zip(String, lists:seq(1, length(String))),
Duplicates = [{Char1, Pos1, Pos2} || {Char1, Pos1} <- CharPosPairs,
{Char2, Pos2} <- CharPosPairs,
Char1 =:= Char2,
Pos2 > Pos1],
case Duplicates of
[] ->
all_unique;
[{Char, P1, P2}|_] ->
{not_all_unique, Char, P1, P2}
end.
 
all_unique_examples() ->
lists:foreach(fun (Str) ->
io:format("String \"~ts\" (length ~p): ",
[Str, length(Str)]),
case all_unique(Str) of
all_unique ->
io:format("All characters unique.~n");
{not_all_unique, Char, P1, P2} ->
io:format("First duplicate is '~tc' (0x~.16b)"
" at positions ~p and ~p.~n",
[Char, Char, P1, P2])
end
end,
["", ".", "abcABC", "XYZ ZYX",
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]).
</lang>
{{out}}
<pre>
$ erl
Erlang/OTP 23 [erts-11.1.8] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]
 
Eshell V11.1.8 (abort with ^G)
1> c(string_examples).
{ok,string_examples}
2> string_examples:all_unique_examples().
String "" (length 0): All characters unique.
String "." (length 1): All characters unique.
String "abcABC" (length 6): All characters unique.
String "XYZ ZYX" (length 7): First duplicate is 'X' (0x58) at positions 1 and 7.
String "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (length 36): First duplicate is '0' (0x30) at positions 10 and 25.
ok
 
</pre>
 
=={{header|F_Sharp|F#}}==
Line 1,180 ⟶ 1,232:
'0' is repeated at positions 9 24 in <<<1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ>>> (length 36)
</pre>
 
=={{header|Factor}}==
<lang factor>USING: formatting fry generalizations io kernel math.parser