Jump to content

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

Added Perl example
m (changed a reference to an in-line reference.)
(Added Perl example)
Line 159:
'🐡' (0x1f421) is duplicated at positions 3 and 8.
</pre>
 
=={{header|Perl}}==
<lang perl>use strict;
use warnings;
use feature 'say';
use utf8;
binmode(STDOUT, ':utf8');
use List::AllUtils qw(uniq);
use Unicode::Normalize qw(NFC);
use Unicode::UCD 'charinfo';
 
for my $str (
'',
'.',
'abcABC',
'XYZ ZYX',
'1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ',
'01234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ0X',
'Δ👍👨👍Δ',
) {
printf qq{\n"$str" (length: %d) has }, my $len = length NFC $str;
if ($len != uniq my @S = split //, NFC $str) {
say "duplicated characters:";
my %P;
push @{ $P{$S[$_]} }, 1+$_ for 0..$#S;
for my $k (sort keys %P) {
next unless @{$P{$k}} > 1;
printf "'%s' %s (0x%x) in positions: %s\n", $k, charinfo(ord $k)->{'name'}, ord($k), join ', ', @{$P{$k}};
}
} else {
say "no duplicated characters."
}
}</lang>
{{out}}
<pre>"" (length: 0) has no duplicated characters.
 
"." (length: 1) has no duplicated characters.
 
"abcABC" (length: 6) has no duplicated characters.
 
"XYZ ZYX" (length: 7) has duplicated characters:
'X' LATIN CAPITAL LETTER X (0x58) in positions: 1, 7
'Y' LATIN CAPITAL LETTER Y (0x59) in positions: 2, 6
'Z' LATIN CAPITAL LETTER Z (0x5a) in positions: 3, 5
 
"1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" (length: 36) has duplicated characters:
'0' DIGIT ZERO (0x30) in positions: 10, 25
 
"01234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ0X" (length: 39) has duplicated characters:
'0' DIGIT ZERO (0x30) in positions: 1, 11, 26, 38
'X' LATIN CAPITAL LETTER X (0x58) in positions: 35, 39
 
"Δ👍👨👍Δ" (length: 5) has duplicated characters:
'Δ' GREEK CAPITAL LETTER DELTA (0x394) in positions: 1, 5
'👍' THUMBS UP SIGN (0x1f44d) in positions: 2, 4</pre>
 
=={{header|Perl 6}}==
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.