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

no edit summary
(→‎{{header|Haskell}}: Simplified a groupBy expression using `on`)
No edit summary
Line 2,162:
"XYZ ZYX" (7): 'X' (0x58) duplicates at 1, 7
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (36): '0' (0x30) duplicates at 10, 25</pre>
 
=={{header|Maple}}==
<lang Maple>CheckUnique:=proc(s)
local i, index;
printf("input: \"%s\", length: %a\n", s, StringTools:-Length(s));
for i from 1 to StringTools:-Length(s) do
index := StringTools:-SearchAll(s[i], s);
if (numelems([index]) > 1) then
printf("The given string has duplicated characters.\n");
printf("The first duplicated character is %a (0x%x) which appears at index %a.\n",
s[i], convert(s[i], 'bytes')[1], {index});
return;
end if;
end do;
# if no repeated found
printf("The given string has all unique characters.\n");
end proc:
 
# Test
CheckUnique("");
CheckUnique(".");
CheckUnique("abcABC");
CheckUnique("XYZ ZYX");
CheckUnique("1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ");</lang>
{{out}}
<pre>
input: "", length: 0
The given string has all unique characters.
input: ".", length: 1
The given string has all unique characters.
input: "abcABC", length: 6
The given string has all unique characters.
input: "XYZ ZYX", length: 7
The given string has duplicated characters.
The first duplicated character is "X" (0x58) which appears at index {1, 7}.
input: "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ", length: 36
The given string has duplicated characters.
The first duplicated character is "0" (0x30) which appears at index {10, 25}.
</pre>
 
=={{header|Nanoquery}}==
Anonymous user